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

// #####   #####   #####   #####   #####   #       #####   #   #//
// #   #   #   #   #       #       #   #   #       #   #   ## ##//
// #####   #####   #####   #       #####   #       #####   # # #//
// ##      #   #       #   #       #   #   #       #   #   #   #//
// # #     #   #       #   #       #   #   #       #   #   #   #//
// #  #    #   #       #   #       #   #   #       #   #   #   #//
// #   #   #   #   #####   #####   #   #   #####   #   #   #   #//

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ppb pop_back
#define cs(n) cout << n << " "
#define cn(n) cout << n << "\\n"
#define rep(i, j, k) for (int i = j; i < k; i++)
#define rrep(i, j, k) for (int i = j; i >= k; i--)
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
int n, k;
vector<ll> q;
ll bin(ll a, ll b, ll mod) {
  if (b == 0)
    return 1;
  if (b % 2 == 0)
    return bin((a * a) % mod, b / 2, mod) % mod;
  return ((a % mod) * (bin((a * a) % mod, b / 2, mod) % mod)) % mod;
}
bool func(int mid) {
  if (mid == 0) {
    return false;
  }
  int c = 0;
  for (auto j : q) {
    if (j <= mid) {
      break;
    }
    c += (ll)(j / mid);
  }
  if (c <= k) {
    return true;
  }
  return false;
}
void solve() {
  cin >> n >> k;
  rep(i, 0, n) {
    ll a;
    cin >> a;
    q.pb(a);
  }
  sort(q.rbegin(), q.rend());
  int lo = 1, hi = q[0];
  while (hi - lo > 1) {
    int mid = (hi + lo) / 2;
    if (func(mid)) {
      hi = mid;
    } else {
      lo = mid + 1;
    }
  }
  if (func(lo)) {
    cout << lo;
  } else {
    cout << hi;
  }
  // for (auto k : q) {
  // 	cout << k << " ";
  // }
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fastio;
  int t;
  t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
#ifndef ONLINE_JUDGE
  cout << "\\nTime Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec\\n";
#endif
  return 0;
}

Error: TLE

Fixed submission:

// #####   #####   #####   #####   #####   #       #####   #   #//
// #   #   #   #   #       #       #   #   #       #   #   ## ##//
// #####   #####   #####   #       #####   #       #####   # # #//
// ##      #   #       #   #       #   #   #       #   #   #   #//
// # #     #   #       #   #       #   #   #       #   #   #   #//
// #  #    #   #       #   #       #   #   #       #   #   #   #//
// #   #   #   #   #####   #####   #   #   #####   #   #   #   #//

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ppb pop_back
#define cs(n) cout << n << " "
#define cn(n) cout << n << "\\n"
#define rep(i, j, k) for (int i = j; i < k; i++)
#define rrep(i, j, k) for (int i = j; i >= k; i--)
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
int n, k;
map<double, int> mp;
vector<double> q;
ll bin(ll a, ll b, ll mod) {
  if (b == 0)
    return 1;
  if (b % 2 == 0)
    return bin((a * a) % mod, b / 2, mod) % mod;
  return ((a % mod) * (bin((a * a) % mod, b / 2, mod) % mod)) % mod;
}
bool func(int mid) {
  double a = mid;
  int k1 = k;
  for (auto j : q) {
    if (j <= a) {
      return true;
    }
    int c1 = 1;
    while (j / (c1 + 1) > a) {
      c1++;
    }
    c1 = c1 * mp[j];
    if (c1 > k1) {
      return false;
    }
    k1 -= c1;
  }
  return true;
}
void solve() {
  cin >> n >> k;
  rep(i, 0, n) {
    double a;
    cin >> a;
    if (mp[a] == 0)
      q.pb(a);
    mp[a]++;
  }
  sort(q.rbegin(), q.rend());
  int lo = 1, hi = 1000000000;
  while (hi - lo > 1) {
    int mid = (hi + lo) / 2;
    if (func(mid)) {
      hi = mid;
    } else {
      lo = mid + 1;
    }
  }
  if (func(lo)) {
    cout << lo;
  } else {
    cout << hi;
  }
  // for (auto k : q) {
  // 	cout << k << " ";
  // }
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fastio;
  int t;
  t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
#ifndef ONLINE_JUDGE
  cout << "\\nTime Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec\\n";
#endif
  return 0;
}