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

#include <bits/stdc++.h>
using namespace std;

/*
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
*/

//************** StrAnge.R *********************

#define ll long long int
#define ull unsigned long long
#define ld long double
#define lll __int128
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vector<int>>
#define pii pair<int, int>
#define piii pair<int, pair<int, int>>
#define pll pair<ll, ll>
#define vii vector<pii>
#define min_pq priority_queue<int, vector<int>, greater<int>>

#define sz(v) ((int)(v).size())
#define all(s) s.begin(), s.end()
#define allr(s) s.rbegin(), s.rend()
#define unq(c) (sort(all(c)), c.resize(distance(c.begin(), unique(all(c)))))
#define get_pos(c, x) (lower_bound(all(c), x) - c.begin())

#define MS0(v) memset((v), 0, sizeof((v)))
#define MS1(v) memset((v), -1, sizeof((v)))
#define LEN(v) strlen(v)

#define MP make_pair
#define pb push_back
#define pob pop_back
#define ff first
#define ss second
#define sc scanf
#define pf printf
#define endl "\\n"

#define intmx INT_MAX
#define llmx 1llu << 61
#define PI 3.14159265358979323846264338327950L
#define MOD 1000000007
#define MAX 100010

#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) ((a) * ((b) / gcd(a, b)))

#define shuffle(v) (random_shuffle(all(v)))
#define min_ele(v) (*min_element(all(v)))
#define max_ele(v) (*max_element(all(v)))
#define is_equal(x, y) (abs(x - y) < eps)
#define cnt_ele(v, x) (count(all(v), x))
#define sum_ele(v) (accumulate(all(v), 0))
#define pro_ele(v) (accumulate(all(v), 1, multiplies<int>()))
#define init_range(v, x) (iota(all(v), x))
#define ran(a, b) ((((rand() << 15) ^ rand()) % ((b) - (a) + 1)) + (a))

#define TEST_CASE                                                              \\
  int ___T;                                                                    \\
  cin >> ___T;                                                                 \\
  for (int cs = 1; cs <= ___T; cs++)
#define PRINT_CASE cout << "Case " << cs << ": ";
#define PRINT_CASEN cout << "Case " << cs << ":\\n";

#define vpf(v)                                                                 \\
  for (int i = 0; i < v.size(); i++) {                                         \\
    cout << v[i];                                                              \\
    if (i != v.size() - 1)                                                     \\
      cout << " ";                                                             \\
    else                                                                       \\
      cout << "\\n";                                                            \\
  }
#define vsc(v)                                                                 \\
  for (int i = 0; i < v.size(); i++)                                           \\
    cin >> v[i];

#define FOR(i, a, b, stp) for (int i = (a); i <= (b); i += stp)
#define ROF(i, a, b, stp) for (int i = (a); i >= (b); i -= stp)

template <class T> inline bool read(T &x) {
  int c = getchar();
  int sgn = 1;
  while (~c && c < '0' || c > '9') {
    if (c == '-')
      sgn = -1;
    c = getchar();
  }
  for (x = 0; ~c && '0' <= c && c <= '9'; c = getchar())
    x = x * 10 + c - '0';
  x *= sgn;
  return ~c;
}
inline ll exp(ll a, ll p) {
  if (p == 0)
    return 1;
  ll x = exp(a, p / 2) % MOD;
  x = (x * x) % MOD;
  if (p & 1)
    x = (x * (a % MOD)) % MOD;
  return x;
}

inline int add(int a, int b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
  return a;
}
inline int sub(int a, int b) {
  a -= b;
  if (a < MOD)
    a += MOD;
  return a;
}
inline int multi(ll a, ll b) {
  a *= b;
  if (a >= MOD)
    a %= MOD;
  return a;
}

inline int on_bit(int N, int pos) { return N = N | (1 << pos); }
inline int off_bit(int N, int pos) { return N = N & ~(1 << pos); }
inline bool check_bit(ll N, int pos) { return (bool)(N & (1 << pos)); }
#define on_bit_cnt(x) (__builtin_popcount(x))

#define start_clock clock_t tStart = clock()
#define end_clock                                                              \\
  printf("\\n>>Runtime: %.10fs\\n", (double)(clock() - tStart) / CLOCKS_PER_SEC)

#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define fileio                                                                 \\
  freopen("in.txt", "r", stdin);                                               \\
  freopen("out.txt", "w", stdout)

//******************* my code starts here **********************************

int smaller_equal(int val, int tar, int lim) {
  int lo = 0, hi = lim, ret = 0;
  while (lo <= hi) {
    int mid = (lo + hi) / 2;

    if (ceil((val * 1.0) / (mid + 1)) <= tar) {
      ret = mid;
      hi = mid - 1;
    } else
      lo = mid + 1;
  }
  return ret;
}

int main() {

  int n, k;
  cin >> n >> k;
  vi v(n);
  vsc(v);

  int lo = 1, hi = 1e9, ans = -1;
  while (lo <= hi) {
    int mid = (lo + hi) / 2;

    int x = k;
    bool flg = 1;
    for (int i = 0; i < n; i++) {
      int y = smaller_equal(v[i], mid, x);
      if (ceil((v[i] * 1.0) / (y + 1)) > mid) {
        flg = 0;
        break;
      }
      x -= y;
    }

    if (flg) {
      ans = mid;
      hi = mid - 1;
    } else
      lo = mid + 1;
  }

  cout << ans << "\\n";

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

/*
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
*/

//************** StrAnge.R *********************

#define ll long long int
#define ull unsigned long long
#define ld long double
#define lll __int128
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vector<int>>
#define pii pair<int, int>
#define piii pair<int, pair<int, int>>
#define pll pair<ll, ll>
#define vii vector<pii>
#define min_pq priority_queue<int, vector<int>, greater<int>>

#define sz(v) ((int)(v).size())
#define all(s) s.begin(), s.end()
#define allr(s) s.rbegin(), s.rend()
#define unq(c) (sort(all(c)), c.resize(distance(c.begin(), unique(all(c)))))
#define get_pos(c, x) (lower_bound(all(c), x) - c.begin())

#define MS0(v) memset((v), 0, sizeof((v)))
#define MS1(v) memset((v), -1, sizeof((v)))
#define LEN(v) strlen(v)

#define MP make_pair
#define pb push_back
#define pob pop_back
#define ff first
#define ss second
#define sc scanf
#define pf printf
#define endl "\\n"

#define intmx INT_MAX
#define llmx 1llu << 61
#define PI 3.14159265358979323846264338327950L
#define MOD 1000000007
#define MAX 100010

#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) ((a) * ((b) / gcd(a, b)))

#define shuffle(v) (random_shuffle(all(v)))
#define min_ele(v) (*min_element(all(v)))
#define max_ele(v) (*max_element(all(v)))
#define is_equal(x, y) (abs(x - y) < eps)
#define cnt_ele(v, x) (count(all(v), x))
#define sum_ele(v) (accumulate(all(v), 0))
#define pro_ele(v) (accumulate(all(v), 1, multiplies<int>()))
#define init_range(v, x) (iota(all(v), x))
#define ran(a, b) ((((rand() << 15) ^ rand()) % ((b) - (a) + 1)) + (a))

#define TEST_CASE                                                              \\
  int ___T;                                                                    \\
  cin >> ___T;                                                                 \\
  for (int cs = 1; cs <= ___T; cs++)
#define PRINT_CASE cout << "Case " << cs << ": ";
#define PRINT_CASEN cout << "Case " << cs << ":\\n";

#define vpf(v)                                                                 \\
  for (int i = 0; i < v.size(); i++) {                                         \\
    cout << v[i];                                                              \\
    if (i != v.size() - 1)                                                     \\
      cout << " ";                                                             \\
    else                                                                       \\
      cout << "\\n";                                                            \\
  }
#define vsc(v)                                                                 \\
  for (int i = 0; i < v.size(); i++)                                           \\
    cin >> v[i];

#define FOR(i, a, b, stp) for (int i = (a); i <= (b); i += stp)
#define ROF(i, a, b, stp) for (int i = (a); i >= (b); i -= stp)

template <class T> inline bool read(T &x) {
  int c = getchar();
  int sgn = 1;
  while (~c && c < '0' || c > '9') {
    if (c == '-')
      sgn = -1;
    c = getchar();
  }
  for (x = 0; ~c && '0' <= c && c <= '9'; c = getchar())
    x = x * 10 + c - '0';
  x *= sgn;
  return ~c;
}
inline ll exp(ll a, ll p) {
  if (p == 0)
    return 1;
  ll x = exp(a, p / 2) % MOD;
  x = (x * x) % MOD;
  if (p & 1)
    x = (x * (a % MOD)) % MOD;
  return x;
}

inline int add(int a, int b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
  return a;
}
inline int sub(int a, int b) {
  a -= b;
  if (a < MOD)
    a += MOD;
  return a;
}
inline int multi(ll a, ll b) {
  a *= b;
  if (a >= MOD)
    a %= MOD;
  return a;
}

inline int on_bit(int N, int pos) { return N = N | (1 << pos); }
inline int off_bit(int N, int pos) { return N = N & ~(1 << pos); }
inline bool check_bit(ll N, int pos) { return (bool)(N & (1 << pos)); }
#define on_bit_cnt(x) (__builtin_popcount(x))

#define start_clock clock_t tStart = clock()
#define end_clock                                                              \\
  printf("\\n>>Runtime: %.10fs\\n", (double)(clock() - tStart) / CLOCKS_PER_SEC)

#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define fileio                                                                 \\
  freopen("in.txt", "r", stdin);                                               \\
  freopen("out.txt", "w", stdout)

//******************* my code starts here **********************************

struct logs {
  int length, cut;
  logs(int _l) {
    length = _l;
    cut = 1;
  }
};

bool operator<(logs x, logs y) {
  int a = ceil((x.length * 1.0) / x.cut);
  int b = ceil((y.length * 1.0) / y.cut);

  return (a < b);
}

int main() {

  priority_queue<logs> pq;
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    pq.push(logs(x));
  }

  if (n == 1) {
    int x = pq.top().length;
    x = ceil((x * 1.0) / (k + 1));
    cout << x << "\\n";
    return 0;
  }

  while (k) {
    logs x = pq.top();
    pq.pop();
    logs y = pq.top();
    pq.pop();

    int lo = 1, hi = k, ans = 1;
    while (lo <= hi) {
      int mid = (lo + hi) / 2;

      logs z = x;
      z.cut += mid;

      if (ceil((z.length * 1.0) / z.cut) <= ceil((y.length * 1.0) / y.cut)) {
        ans = mid;
        hi = mid - 1;
      } else
        lo = mid + 1;
    }

    x.cut += ans;
    k -= ans;
    pq.push(x);
    pq.push(y);
  }

  cout << (int)(ceil((pq.top().length * 1.0) / pq.top().cut)) << "\\n";

  return 0;
}