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

#include <algorithm>
#include <atomic>
#include <bitset>
#include <chrono>
#include <condition_variable>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <memory>
#include <mutex>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <string_view>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

static int my_speed_up = []() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  return 0;
}();
using ll = long long;
using ull = unsigned long long;
template <class T> using v = vector<T>;
template <class T> using vv = vector<vector<T>>;
template <class T> using vvv = vector<vector<vector<T>>>;
template <class T> using vvvv = vector<vector<vector<vector<T>>>>;
#define for_b(i, b) for (int i = 0; i < (b); ++i)
#define for_ab(i, a, b) for (int i = a; i < (b); ++i)
#define what_is(x) cout << #x << " = " << x << endl;
#define what_are(x, y)                                                         \\
  cout << #x << " = " << x << ", " << #y << " = " << y << endl;
#define pad(x) for_b(i, x) cout << "  ";
#define show_1d(___x)                                                          \\
  cout << #___x << " = ";                                                      \\
  for (auto &___num : ___x)                                                    \\
    cout << ___num << ", ";                                                    \\
  cout << endl;
#define show_2d(___x)                                                          \\
  cout << #___x << endl;                                                       \\
  for (auto &___row : ___x) {                                                  \\
    for (auto ___num : ___row)                                                 \\
      cout << ___num << ", ";                                                  \\
    cout << endl;                                                              \\
  }
#define show_3d(___x)                                                          \\
  cout << #___x << endl;                                                       \\
  for (auto &___row : ___x) {                                                  \\
    for (auto &___row2 : ___row) {                                             \\
      for (auto ___num : ___row2)                                              \\
        cout << ___num << ", ";                                                \\
      cout << endl;                                                            \\
    }                                                                          \\
    cout << endl;                                                              \\
  }

int main() {
  int n, k;
  cin >> n >> k;

  auto cmp = [](const pair<double, int> &a, const pair<double, int> &b) {
    return a.first < b.first;
  };
  std::priority_queue<pair<double, int>, std::vector<pair<double, int>>,
                      decltype(cmp)>
      tb(cmp);

  double avg = 0;
  vv<int> logs(n, v<int>(2));
  for_b(i, n) {
    cin >> logs[i][0];
    logs[i][1] = 1;

    avg += logs[i][0];
    tb.push(make_pair(logs[i][0], i));
  }
  avg /= double(n + k);

  for_b(i, k) {
    int idx = tb.top().second;
    tb.pop();

    int take = double(logs[idx][0]) / avg;
    take -= logs[idx][1];
    take = max(take, 1);
    i += take - 1;
    logs[idx][1] += take;
    tb.push(make_pair(double(logs[idx][0]) / double(logs[idx][1]), idx));
  }

  int res_idx = tb.top().second;

  double res = double(logs[res_idx][0]) / double(logs[res_idx][1]);
  int resi = res;
  if (resi < res)
    ++resi;

  cout << resi << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <atomic>
#include <bitset>
#include <chrono>
#include <condition_variable>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <memory>
#include <mutex>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <string_view>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

static int my_speed_up = []() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  return 0;
}();
using ll = long long;
using ull = unsigned long long;
template <class T> using v = vector<T>;
template <class T> using vv = vector<vector<T>>;
template <class T> using vvv = vector<vector<vector<T>>>;
template <class T> using vvvv = vector<vector<vector<vector<T>>>>;
#define for_b(i, b) for (int i = 0; i < (b); ++i)
#define for_ab(i, a, b) for (int i = a; i < (b); ++i)
#define what_is(x) cout << #x << " = " << x << endl;
#define what_are(x, y)                                                         \\
  cout << #x << " = " << x << ", " << #y << " = " << y << endl;
#define pad(x) for_b(i, x) cout << "  ";
#define show_1d(___x)                                                          \\
  cout << #___x << " = ";                                                      \\
  for (auto &___num : ___x)                                                    \\
    cout << ___num << ", ";                                                    \\
  cout << endl;
#define show_2d(___x)                                                          \\
  cout << #___x << endl;                                                       \\
  for (auto &___row : ___x) {                                                  \\
    for (auto ___num : ___row)                                                 \\
      cout << ___num << ", ";                                                  \\
    cout << endl;                                                              \\
  }
#define show_3d(___x)                                                          \\
  cout << #___x << endl;                                                       \\
  for (auto &___row : ___x) {                                                  \\
    for (auto &___row2 : ___row) {                                             \\
      for (auto ___num : ___row2)                                              \\
        cout << ___num << ", ";                                                \\
      cout << endl;                                                            \\
    }                                                                          \\
    cout << endl;                                                              \\
  }

int main() {
  int n, k;
  cin >> n >> k;

  auto cmp = [](const pair<double, int> &a, const pair<double, int> &b) {
    return a.first < b.first;
  };
  std::priority_queue<pair<double, int>, std::vector<pair<double, int>>,
                      decltype(cmp)>
      tb(cmp);

  double avg = 0;
  vv<int> logs(n, v<int>(2));
  for_b(i, n) {
    cin >> logs[i][0];
    logs[i][1] = 1;

    avg += logs[i][0];
    tb.push(make_pair(logs[i][0], i));
  }
  avg /= double(n + k);

  for_b(i, k) {
    int idx = tb.top().second;
    tb.pop();

    int take =
        (double(logs[idx][0]) / double(logs[idx][1])) /
        (double(logs[tb.top().second][0]) / double(logs[tb.top().second][1]));
    logs[idx][1] += take < (k - i) ? take : (k - i);
    i += take - 1;
    tb.push(make_pair(double(logs[idx][0]) / double(logs[idx][1]), idx));
  }

  int res_idx = tb.top().second;

  double res = double(logs[res_idx][0]) / double(logs[res_idx][1]);
  int resi = res;
  if (resi < res)
    ++resi;

  cout << resi << endl;

  return 0;
}