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

#include <bits/stdc++.h>

#define TYPE(c) remove_reference_t<decltype(c)>
#define REP(i, n) for (TYPE(n) i = 0; i < n; i++)
#define FOR(v, c) for (TYPE(c.begin()) v = c.begin(); v != c.end(); v++)
#define ALL(c) c.begin(), c.end()
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(ALL(c), greater<TYPE(c)::value_type>())
#define UNIQUE(c) c.erase(unique(ALL(c)), c.end())

using namespace std;
using ll = long long;

constexpr int MOD = (int)1e9 + 7;
constexpr int INF = (int)1e9 + 1;
constexpr ll LINF = (ll)1e18 + 1;
template <typename T> constexpr bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <typename T> constexpr bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <typename S, typename T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << "(" << p.first << "," << p.second << ")";
}
ostream &operator<<(ostream &os, const string &s) {
  FOR(v, s) os << *v;
  return os;
}
template <typename T>
auto operator<<(ostream &os, const T &t) ->
    typename conditional<true, ostream &, decltype(*t.begin())>::type {
  os << "[";
  FOR(v, t) {
    if (v != t.begin())
      os << ",";
    os << *v;
  }
  os << "]";
  return os;
}

bool cnt(const std::vector<int> &A, const int len, const int K) {
  ll cnt = 0;
  for (const auto &v : A) {
    cnt += ceil(v / (double)len) - 1;
  }
  return cnt <= K;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  std::cin >> N;
  int K;
  std::cin >> K;
  std::vector<int> A(N);
  REP(i, N) { std::cin >> A[i]; }

  int l = 0;
  int r = *max_element(ALL(A));
  while (abs(l - r) > 1) {
    auto m = (l + r) / 2;
    if (cnt(A, m, K)) {
      r = m;
    } else {
      l = m;
    }
  }
  std::cout << r << std::endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define TYPE(c) remove_reference_t<decltype(c)>
#define REP(i, n) for (TYPE(n) i = 0; i < n; i++)
#define FOR(v, c) for (TYPE(c.begin()) v = c.begin(); v != c.end(); v++)
#define ALL(c) c.begin(), c.end()
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(ALL(c), greater<TYPE(c)::value_type>())
#define UNIQUE(c) c.erase(unique(ALL(c)), c.end())

using namespace std;
using ll = long long;

constexpr int MOD = (int)1e9 + 7;
constexpr int INF = (int)1e9 + 1;
constexpr ll LINF = (ll)1e18 + 1;
template <typename T> constexpr bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <typename T> constexpr bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <typename S, typename T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << "(" << p.first << "," << p.second << ")";
}
ostream &operator<<(ostream &os, const string &s) {
  FOR(v, s) os << *v;
  return os;
}
template <typename T>
auto operator<<(ostream &os, const T &t) ->
    typename conditional<true, ostream &, decltype(*t.begin())>::type {
  os << "[";
  FOR(v, t) {
    if (v != t.begin())
      os << ",";
    os << *v;
  }
  os << "]";
  return os;
}

bool cnt(const std::vector<int> &A, const double len, const int K) {
  ll cnt = 0;
  for (const auto &v : A) {
    cnt += ceil(v / len) - 1;
  }
  return cnt <= K;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  std::cin >> N;
  int K;
  std::cin >> K;
  std::vector<int> A(N);
  REP(i, N) { std::cin >> A[i]; }

  double l = 0;
  double r = *max_element(ALL(A));
  while (abs(l - r) > 1e-10) {
    auto m = (l + r) / 2;
    auto is_ok = cnt(A, m, K);
    if (is_ok) {
      r = m;
    } else {
      l = m;
    }
  }
  std::cout << (int)ceil(l) << std::endl;

  return 0;
}