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

//
// Created on 2020/08/02.
//

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <iomanip>
#include <ios>
#include <iostream> // cout, endl, cin
#include <map>      // map
#include <math.h>
#include <numeric>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;
using namespace std;
const long long LINF = 1e18;
const int INF = 1e9;

int main() {

  int n;
  int k;
  cin >> n >> k;
  vector<double> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  double ok = INF;
  double ng = 0;
  double mid = (ok + ng) / 2;
  while (0.1 < abs(ok - ng)) {
    int cnt = 0;
    for (int i = 0; i < n; ++i) {
      cnt += (int)((a[i] + (mid - 1)) / mid) - 1;
    }
    if (cnt <= k) {
      ok = mid;
    } else {
      ng = mid;
    }
    mid = (ok + ng) / 2;
  }
  int ans = ceil(ok);
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

//
// Created on 2020/08/02.
//

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <iomanip>
#include <ios>
#include <iostream> // cout, endl, cin
#include <map>      // map
#include <math.h>
#include <numeric>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;
using namespace std;
const long long LINF = 1e18;
const int INF = 1e9;

int main() {

  int n;
  int k;
  cin >> n >> k;
  vector<double> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  double ok = INF;
  double ng = 1;
  double mid = (ok + ng) / 2;
  int t = 100000;
  while (t) {
    int cnt = 0;
    for (int i = 0; i < n; ++i) {
      cnt += (int)((a[i] + (mid - 1)) / mid) - 1;
    }
    if (cnt <= k) {
      ok = mid;
    } else {
      ng = mid;
    }
    mid = (ok + ng) / 2;
    t--;
  }
  int ans = ceil(ok);
  cout << ans << endl;

  return 0;
}