Problem p02598 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <chrono>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define endl "\\n"
#define fi first
#define se second
#define pll pair<ll, ll>
#define forn(i, e) for (ll i = 1; i <= e; i++)
#define forsn(i, s, e) for (ll i = s; i <= e; i++)
#define rforn(i, s) for (ll i = s; i >= 0; i--)
#define rforsn(i, s, e) for (ll i = s; i >= e; i--)
#define dbg(x) cout << #x << " = " << x << endl
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
// freopen("input.txt","r", stdin);
// freopen("out.txt","w", stdout);
// auto start = chrono::high_resolution_clock::now();
// auto end = chrono::high_resolution_clock::now();
// double time_taken =chrono::duration_cast<chrono::nanoseconds>(end -
// start).count(); time_taken *= 1e-9; cout << "Time taken by program is : " <<
// fixed << time_taken << setprecision(9); cout << " sec" << endl;
ll n, k, a[200010];
bool pred(double x) {
  ll cut = 0;
  for (ll i = 1; i <= n; i++) {
    cut = cut + floor(double(a[i]) / x);
    if (cut > k)
      return false;
  }
  return true;
}
int main() {
  cin >> n >> k;
  ll mx = 0;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
    mx = max(a[i], mx);
  }
  if (k == 0) {
    cout << mx << endl;
    return 0;
  }
  double l = 0, h = 1e9 + 10;
  while (h - l > 0.00001) {
    double mid = (l + h) / 2;
    if (pred(mid))
      h = mid;
    else
      l = mid;
  }
  if (h - floor(h) < 0.0001) {
    h = floor(h);
    if (h < 1)
      h = 1;
  }
  cout << setprecision(10) << ceil(h) << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <chrono>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define endl "\\n"
#define fi first
#define se second
#define pll pair<ll, ll>
#define forn(i, e) for (ll i = 1; i <= e; i++)
#define forsn(i, s, e) for (ll i = s; i <= e; i++)
#define rforn(i, s) for (ll i = s; i >= 0; i--)
#define rforsn(i, s, e) for (ll i = s; i >= e; i--)
#define dbg(x) cout << #x << " = " << x << endl
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
// freopen("input.txt","r", stdin);
// freopen("out.txt","w", stdout);
// auto start = chrono::high_resolution_clock::now();
// auto end = chrono::high_resolution_clock::now();
// double time_taken =chrono::duration_cast<chrono::nanoseconds>(end -
// start).count(); time_taken *= 1e-9; cout << "Time taken by program is : " <<
// fixed << time_taken << setprecision(9); cout << " sec" << endl;
ll n, k, a[200010];
bool pred(double x) {
  ll cut = 0;
  for (ll i = 1; i <= n; i++) {
    cut = cut + floor(double(a[i]) / x);
    if (cut > k)
      return false;
  }
  return true;
}
int main() {
  cin >> n >> k;
  ll mx = 0;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
    mx = max(a[i], mx);
  }
  if (k == 0) {
    cout << mx << endl;
    return 0;
  }
  double l = 0, h = 1e9 + 10;
  while (h - l > 0.0000001) {
    double mid = (l + h) / 2;
    if (pred(mid))
      h = mid;
    else
      l = mid;
  }
  if (h - floor(h) < 0.000001)
    h = floor(h);
  cout << setprecision(18) << ceil(h) << endl;
}