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

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <new>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define EPS (1e-9)
#define INF (1e9)
#define MOD (ll)(1e9 + 7)
#define PI (acos(-1))
#define REP(i, a, n) for (int i = a; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define allof(a) (a).begin(), (a).end()

#define Yes(q) ((q) ? "Yes" : "No")
#define YES(q) ((q) ? "YES" : "NO")
#define Possible(q) ((q) ? "Possible" : "Impossible")
#define POSSIBLE(q) ((q) ? "POSSIBLE" ; "IMPOSSIBLE")

using ll = long long int;

ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll beki(ll a, ll b) {
  ll tmp = 1;
  rep(i, b) tmp *= a;
  return tmp;
}

ll modPow(ll x, ll a) {
  if (a == 1)
    return x;
  if (a % 2)
    return (x * modPow(x, a - 1)) % MOD;
  ll t = modPow(x, a / 2);
  return ((t % MOD) * (t % MOD)) % MOD;
}

ll modInv(ll x) { return modPow(x, MOD - 2); }

ll N, K;
long double A[200010];

int main() {
  cin >> N >> K;
  rep(i, N) { cin >> A[i]; }
  sort(A, A + N, greater<long double>());

  ll l = 0, r = A[0];

  while (l != r - 1) {
    ll c = (l + r) / 2;
    // ここでK回切ってc以下を達成できるか調べる
    ll k = 0;
    bool check = true;
    rep(i, N) {
      k += (ll)ceil(A[i] / c) - 1;
      if (k > K) {
        check = false;
        break;
      }
    }
    if (check)
      r = c;
    else
      l = c;
  }
  cout << r << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <new>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define EPS (1e-9)
#define INF (1e9)
#define MOD (ll)(1e9 + 7)
#define PI (acos(-1))
#define REP(i, a, n) for (int i = a; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define allof(a) (a).begin(), (a).end()

#define Yes(q) ((q) ? "Yes" : "No")
#define YES(q) ((q) ? "YES" : "NO")
#define Possible(q) ((q) ? "Possible" : "Impossible")
#define POSSIBLE(q) ((q) ? "POSSIBLE" ; "IMPOSSIBLE")

using ll = long long int;

ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll beki(ll a, ll b) {
  ll tmp = 1;
  rep(i, b) tmp *= a;
  return tmp;
}

ll modPow(ll x, ll a) {
  if (a == 1)
    return x;
  if (a % 2)
    return (x * modPow(x, a - 1)) % MOD;
  ll t = modPow(x, a / 2);
  return ((t % MOD) * (t % MOD)) % MOD;
}

ll modInv(ll x) { return modPow(x, MOD - 2); }

ll N, K;
long double A[200010];

int main() {
  cin >> N >> K;
  rep(i, N) { cin >> A[i]; }
  sort(A, A + N, greater<long double>());

  ll l = 0, r = A[0];

  while (l != r - 1) {
    ll c = (l + r) / 2;
    // ここでK回切ってc以下を達成できるか調べる
    ll k = 0;
    bool check = true;
    rep(i, N) {
      ll j = 1;
      if (ceil(A[i] / (long double)j) <= c && j == 1)
        break;
      while (ceil(A[i] / (long double)j) > c) {
        if (k < K) {
          j++;
          k++;
        } else {
          check = false;
          break;
        }
      }
    }
    if (check)
      r = c;
    else
      l = c;
  }
  cout << r << endl;
}