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

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <cmath>
#include <deque>
#include <iomanip>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <tuple>
#include <unordered_map>

#define rep(i, x) for (int i = 0; i < (int)(x); i++)
#define FOR(i, a, done) for (int i = (a); i < (done); ++i)
#define all(a) (a).begin(), (a).end()
#define x first
#define y second
#define debug(a) cout << (a) << endl
namespace mp = boost::multiprecision;
using cpp_int = mp::cpp_int;
using ll = long long;
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

ll M = 1000000007;
using tdii = tuple<double, int, int>;
void solve() {

  int n, k;
  cin >> n >> k;
  vector<double> a(n);
  double x = 0;
  rep(i, n) {
    cin >> a[i];
    x = max(x, a[i]);
  }
  if (k == 0) {
    cout << (int)x << endl;
    return;
  }
  double l = 0;
  double ans = 1e9;
  while (x - l > 0.001) {
    int cnt = 0;
    double mid = (l + x) / 2.0;
    rep(i, n) { cnt += (int)(a[i] - 1) / mid; }
    if (cnt <= k) {
      ans = min(ans, mid);
      x = mid;
    } else if (cnt > k) {
      l = mid;
    }
  }

  cout << (int)ceil(ans) << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <cmath>
#include <deque>
#include <iomanip>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <tuple>
#include <unordered_map>

#define rep(i, x) for (int i = 0; i < (int)(x); i++)
#define FOR(i, a, done) for (int i = (a); i < (done); ++i)
#define all(a) (a).begin(), (a).end()
#define x first
#define y second
#define debug(a) cout << (a) << endl
namespace mp = boost::multiprecision;
using cpp_int = mp::cpp_int;
using ll = long long;
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

ll M = 1000000007;
using tdii = tuple<double, int, int>;
void solve() {
  priority_queue<tdii> logs;
  int n, k;
  cin >> n >> k;
  int dis = k / n;
  rep(i, n) {
    int a;
    cin >> a;
    if (dis >= 100) {
      logs.push(tdii((double)a / 100.0, a, 101));
      k -= 100;
    } else {
      logs.push(tdii(a, a, 1));
    }
  }
  rep(i, k) {
    auto [now, init, cuts] = logs.top();
    logs.pop();
    cuts++;
    now = (double)init / cuts;
    logs.push(tdii(now, init, cuts));
  }
  auto [now, init, cuts] = logs.top();
  cout << int(ceil(now)) << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  solve();
  return 0;
}