alexjercan/bugnet huggingface dataset:
Problem p02598 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n, k, a[200005];

bool ok(int c) {
  int cnt = 0;
  for (int i = 0; i < n; i++)
    cnt += a[i] / c;
  return cnt <= k;
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  int l = 0, r = 1001001001;
  while (r - l > 1) {
    int c = (l + r) / 2;
    (ok(c) ? r : l) = c;
  }
  cout << r << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using P = pair<int, pair<int, int>>;

int main() {
  int n, k;
  cin >> n >> k;
  priority_queue<P> q;
  for (int i = 0; i < n; i++) {
    P a;
    cin >> a.first;
    a.second.first = a.first;
    a.second.second = 1;
    q.push(a);
  }
  P now;
  for (int i = 0; i < k; i++) {
    now = q.top();
    q.pop();
    now.second.second++;
    now.first = (now.second.first + now.second.second - 1) / now.second.second;
    q.push(now);
  }
  cout << q.top().first << endl;
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

/*
 * code by: imasnegi
 **/

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

#define ll long long
#define nl "\\n"
#define spc " "
#define fr(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define vii vector<int>
#define vll vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define deb(a) cout << #a << " " << a << nl;

template <typename T> void printArray(vector<T> &v) {
  for (auto &i : v)
    cout << i << spc;
  cout << nl;
}
const int MaxN = 2e5 + 5;
const int MOD = 1e9 + 7;

ll n, k;
ll a[MaxN];

void solve_cases() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];

  ll l = 1, r = 1e9;
  ll ans = -1;
  ll cnt = 0;

  while (l <= r) {
    ll mid = (l + r) / 2;
    cnt = 0;
    for (int i = 0; i < n; i++) {
      if (a[i] > mid) {
        cnt += a[i] / mid;
        if (cnt % mid == 0)
          cnt--;
      }
    }

    if (cnt <= k) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }

  cout << ans << nl;
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cout << setprecision(10) << fixed;
  int t = 1;
  // cin >> t;
  FOR(tt, 1, t) {
    // cout << "Case #" << tt << ": ";
    solve_cases();
  }
}

Error: 0

Fixed submission:

/*
 * code by: imasnegi
 **/

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

#define ll long long
#define nl "\\n"
#define spc " "
#define fr(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define vii vector<int>
#define vll vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define deb(a) cout << #a << " " << a << nl;

template <typename T> void printArray(vector<T> &v) {
  for (auto &i : v)
    cout << i << spc;
  cout << nl;
}
const int MaxN = 2e5 + 5;
const int MOD = 1e9 + 7;

ll n, k;
ll a[MaxN];

void solve_cases() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];

  ll l = 0, r = 1e9;
  ll ans = -1;
  while (l <= r) {
    ll mid = (l + r) / 2;
    int flag = 0;
    ll cnt = 0;
    for (int i = 0; i < n; i++) {
      if (a[i] > mid) {
        cnt += a[i] / mid;
      }
    }

    if (cnt <= k) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }

  cout << ans << nl;
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cout << setprecision(10) << fixed;
  int t = 1;
  // cin >> t;
  FOR(tt, 1, t) {
    // cout << "Case #" << tt << ": ";
    solve_cases();
  }
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define owo(i, a, b) for (int i = (a); i < (b); ++i)
#define uwu(i, a, b) for (int i = (a)-1; i >= (b); --i)
#define senpai push_back
#define ttgl pair<int, int>
#define ayaya cout << "ayaya~" << endl

using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
gpu_hash_map<int, int> mp;*/
using ll = long long;
using ld = long double;
const ll MOD = 1000000007;
const ll root = 62;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
ll binpow(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % MOD;
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}
ll modInv(ll a) { return binpow(a, MOD - 2); }
const double PI = acos(-1);
const double eps = -1e6;
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const ll NINFLL = 0xc0c0c0c0c0c0c0c0;
const int mxN = 200001;
ll n, k;
ll arr[mxN];
int main() {
  // freopen("file.in", "r", stdin);
  // freopen("file.out", "w", stdout);
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  cin.tie(0)->sync_with_stdio(0);
  cin >> n >> k;
  owo(i, 0, n) { cin >> arr[i]; }
  ll l = 1;
  ll r = INF;
  while (l < r) {
    ll mid = (l + r) / 2;
    ll need = 0;
    owo(i, 0, n) { need += ((arr[i] + mid - 1) / mid) - 1; }
    if (need <= k)
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << "\\n";
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define owo(i, a, b) for (int i = (a); i < (b); ++i)
#define uwu(i, a, b) for (int i = (a)-1; i >= (b); --i)
#define senpai push_back
#define ttgl pair<int, int>
#define ayaya cout << "ayaya~" << endl

using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
gpu_hash_map<int, int> mp;*/
using ll = long long;
using ld = long double;
const ll MOD = 1000000007;
const ll root = 62;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
ll binpow(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % MOD;
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}
ll modInv(ll a) { return binpow(a, MOD - 2); }
const double PI = acos(-1);
const double eps = -1e6;
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const ll NINFLL = 0xc0c0c0c0c0c0c0c0;
const int mxN = 200001;
ll n, k;
ll arr[mxN];
int main() {
  // freopen("file.in", "r", stdin);
  // freopen("file.out", "w", stdout);
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  cin.tie(0)->sync_with_stdio(0);
  cin >> n >> k;
  owo(i, 0, n) { cin >> arr[i]; }
  ll l = 0;
  ll r = INF;
  while (l < r) {
    ll mid = (l + r) / 2;
    ll need = 0;
    owo(i, 0, n) { need += (arr[i] + mid - 1) / mid - 1; }
    if (need <= k)
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << "\\n";
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

using namespace std;
using lint = long long;
constexpr int MOD = 1000000007, INF = 1010101010;
constexpr lint LINF = 1LL << 60;

#ifdef _DEBUG
#include "../../library/library/debug_template.cpp"
#define DMP(...) dump(#__VA_ARGS__, __VA_ARGS__)
#else
#define DMP(...) ((void)0)
#endif

struct init {
  init() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  }
} init_;

int main() {

  int N, K;
  cin >> N >> K;

  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];

  auto judge = [&](auto idx) {
    lint sum = 0;
    for (int i = 0; i < N; i++)
      sum += (A[i] - 1) / idx;
    return sum <= K;
  };

  auto binary_search = [&](auto f) {
    auto ng = 0;
    auto ok = INF;

    while (abs(ok - ng) > 1) {
      auto mid = (ok + ng) / 2;

      if (f(mid))
        ok = mid;
      else
        ng = mid;
    }

    return ok;
  };

  cout << binary_search(judge) << "\\n";

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

using namespace std;
using lint = long long;
constexpr int MOD = 1000000007, INF = 1010101010;
constexpr lint LINF = 1LL << 60;

#ifdef _DEBUG
#include "../../library/library/debug_template.cpp"
#define DMP(...) dump(#__VA_ARGS__, __VA_ARGS__)
#else
#define DMP(...) ((void)0)
#endif

struct init {
  init() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  }
} init_;

int main() {

  int N, K;
  cin >> N >> K;

  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];

  auto judge = [&](auto idx) {
    lint sum = 0;
    for (int i = 0; i < N; i++)
      sum += (A[i] - 1) / idx;
    return sum <= K;
  };

  auto binary_search = [&](auto f) {
    auto ng = -1;
    auto ok = INF;

    while (abs(ok - ng) > 1) {
      auto mid = (ok + ng) / 2;

      if (f(mid))
        ok = mid;
      else
        ng = mid;
    }

    return ok;
  };

  cout << binary_search(judge) << "\\n";

  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back

// g++ -std=c++17 -Wshadow -Wall -g -fsanitize=address -fsanitize=undefined
// -D_GLIBCXX_DEBUG -o

int main() {
  ll n, k;
  cin >> n >> k;
  ll a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll r = *max_element(a, a + n);
  ll l = 1;
  while (l < r) {
    ll mid = l + (r - l) / 2;
    ll cuts = 0;
    for (int i = 0; i < n; i++) {
      if (a[i] % mid)
        cuts += (a[i] / mid);
      else
        cuts += ((a[i] - 1) / mid);
    }
    if (cuts > k)
      l = mid + 1;
    else {
      r = mid;
    }
    // cout<<l<<" "<<r<<"\\n";
  }
  cout << l << "\\n";
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back

// g++ -std=c++17 -Wshadow -Wall -g -fsanitize=address -fsanitize=undefined
// -D_GLIBCXX_DEBUG -o

int main() {
  ll n, k;
  cin >> n >> k;
  ll a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll r = *max_element(a, a + n);
  ll l = 0;
  while (l < r) {
    ll mid = l + (r - l) / 2;
    ll cuts = 0;
    for (int i = 0; i < n; i++) {
      if (a[i] % mid)
        cuts += (a[i] / mid);
      else
        cuts += ((a[i] - 1) / mid);
    }
    if (cuts > k)
      l = mid + 1;
    else {
      r = mid;
    }
    // cout<<l<<" "<<r<<"\\n";
  }
  cout << l << "\\n";
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define ALL(n) begin(n), end(n)
#define INF ((1 << 30) - 1)
#define LINF (1LL << 60)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;
typedef pair<int, int> pint;
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;

bool judge(Int cut, vector<Int> logs, Int k) {
  Int ans = 0;
  if (cut == 0)
    return false;
  for (int i = 0; i < logs.size(); i++) {
    if (logs[i] % cut != 0)
      ans += (logs[i] / cut);
    else
      ans += (logs[i] / cut - 1);
    if (ans > k)
      return false;
  }
  if (ans > k)
    return false;
  return true;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  Int n, k;
  cin >> n >> k;
  vector<Int> a(n, 0);
  rep(i, n) cin >> a[i];
  Int ok = 1000000100;
  Int ng = -1;
  while (abs(ok - ng) > 1) {
    Int mid = (ok + ng) / 2;
    if (judge(mid, a, k)) {
      ok = mid;
    } else {
      ng = mid;
    }
  }
  cout << ok << endl;
  return 0;
}

Error: 0

Fixed submission:


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define ALL(n) begin(n), end(n)
#define INF ((1 << 30) - 1)
#define LINF (1LL << 60)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;
typedef pair<int, int> pint;
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;

bool judge(Int cut, vector<Int> logs, Int k) {
  Int ans = 0;
  for (int i = 0; i < logs.size(); i++) {
    if (logs[i] % cut != 0)
      ans += (logs[i] / cut);
    else
      ans += (logs[i] / cut - 1);
    if (ans > k)
      return false;
  }
  if (ans > k)
    return false;
  return true;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  Int n, k;
  cin >> n >> k;
  vector<Int> a(n, 0);
  rep(i, n) cin >> a[i];
  Int ok = 1000000100;
  Int ng = -1;
  while (abs(ok - ng) > 1) {
    Int mid = (ok + ng) / 2;
    if (judge(mid, a, k)) {
      ok = mid;
    } else {
      ng = mid;
    }
  }
  cout << ok << endl;
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int n, k;
vector<int> a(200000);

bool good(int x) {
  int cut = 0;
  for (int i = 0; i < n; i++) {
    cut += a[i] / x;
    if (a[i] % x == 0)
      cut--;
    if (cut > k)
      return false;
  }
  return true;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int upper = *max_element(a.begin(), a.end());
  int lower = 0;
  int mid = 1;
  while (upper - lower > 1) {
    mid = (upper + lower) / 2;
    if (good(mid)) {
      upper = mid;
    } else {
      lower = mid;
    }
  }
  cout << upper << '\\n';
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int n, k;
vector<int> a(20000);

bool good(int x) {
  int cut = 0;
  for (int i = 0; i < n; i++) {
    cut += a[i] / x;
    if (a[i] % x == 0)
      cut--;
    if (cut > k)
      return false;
  }
  return true;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int upper = *max_element(a.begin(), a.end());
  int lower = 0;
  int mid = 1;
  while (upper - lower > 1) {
    mid = (upper + lower) / 2;
    if (good(mid)) {
      upper = mid;
    } else {
      lower = mid;
    }
  }
  cout << upper << '\\n';
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n";
using namespace std;
typedef long long int ll;

bool possible(int mid, vector<int> a, int k) {
  for (auto i : a) {
    k -= (i + mid - 1) / mid - 1;
  }
  if (k >= 0)
    return true;
  return false;
}
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int mn = 0, mx = 1000000001;
  while (mn < mx - 1) {
    int mid = (mn) + (mx - mn) / 2;
    if (possible(mid, a, k))
      mx = mid;
    else
      mn = mid;
  }
  cout << mx << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n";
using namespace std;
typedef long long int ll;
bool possible(int mid, vector<int> a, int k) {
  for (auto i : a) {
    k -= (i + mid - 1) / mid - 1;
  }
  if (k >= 0)
    return true;
  return false;
}
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int mn = 0, mx = (int)1e9 + 100;
  while (mn != mx) {
    int mid = (mn) + (mx - mn) / 2;
    if (possible(mid, a, k))
      mx = mid;
    else
      mn = mid + 1;
  }
  cout << mx << endl;
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdio>
#define rg32 register int
#define rg8 register char
typedef long long ll;
const double eps = 1e-6;
int n, k;
int a[200005];
inline int read() {
  rg32 x = 0, f = 1;
  rg8 s = getchar();
  while (s > '9' || s < '0') {
    if (s == '-')
      f = -1;
    s = getchar();
  }
  while (s >= '0' && s <= '9') {
    x = x * 10 + s - '0';
    s = getchar();
  }
  return x * f;
}
inline bool check(double x) {
  ll sum = 0;
  // for(register int i=1;i<=n;++i) sum+=(int)ceil(a[i]*1.00/x)-1;
  for (rg32 i = 1; i <= n; ++i) {
    ll val = a[i] / x;
    if (fabs(a[i] - x * val) > eps)
      ++val;
    sum += val - 1;
  }
  return sum <= k;
}
int main() {
  n = read(), k = read();
  for (rg32 i = 1; i <= n; ++i)
    a[i] = read();
  double l = 1e-5, r = 1e9;
  while (r - l > eps) {
    double mid = l + (r - l) / 2;
    if (check(mid)) {
      r = mid;
    } else {
      l = mid;
    }
  }
  printf("%lld\\n", (ll)(r + 1 - eps));
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#define rg32 register int
#define rg8 register char
typedef long long ll;
int n, k, res = 0;
int a[200005];
inline int read() {
  rg32 x = 0, f = 1;
  rg8 s = getchar();
  while (s > '9' || s < '0') {
    if (s == '-')
      f = -1;
    s = getchar();
  }
  while (s >= '0' && s <= '9') {
    x = x * 10 + s - '0';
    s = getchar();
  }
  return x * f;
}
inline bool check(double x) {
  ll sum = 0;
  for (register int i = 1; i <= n; ++i)
    sum += (int)ceil(a[i] * 1.00 / x) - 1;
  return sum <= k;
}
int main() {
  n = read(), k = read();
  for (register int i = 1; i <= n; ++i) {
    a[i] = read();
    res = std::max(res, a[i]);
  }
  double l = 0, r = 1e9;
  while (r - l > 1e-7) {
    double mid = l + (r - l) / 2;
    if (check(mid)) {
      res = std::min(res, (int)ceil(mid));
      r = mid;
    } else {
      l = mid;
    }
  }
  printf("%d\\n", res);
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;

#define fi first
#define se second

const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);

void yes() { printf("yes\\n"); }
void no() { printf("no\\n"); }
void Yes() { printf("Yes\\n"); }
void No() { printf("No\\n"); }
void YES() { printf("YES\\n"); }
void NO() { printf("NO\\n"); }

ll N, K;
ll A[int(2e5 + 5)];

bool Solve(ll now) {
  ll cnt = 0;
  for (int i = 0; i < N; i++) {
    cnt += (A[i] - 1) / now;
  }
  if (cnt <= K)
    return true;
  else
    return false;
}

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> A[i];

  ll L = 0, R = 1e16;
  while (R - L > 1) {
    ll now = (L + R) / 2;
    if (Solve(now))
      R = now;
    else
      L = now;
  }
  cout << R << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;

#define fi first
#define se second

const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);

void yes() { printf("yes\\n"); }
void no() { printf("no\\n"); }
void Yes() { printf("Yes\\n"); }
void No() { printf("No\\n"); }
void YES() { printf("YES\\n"); }
void NO() { printf("NO\\n"); }

ll N, K;
ll A[int(1e5 + 5)];

bool Solve(ll now) {
  ll cnt = 0;
  for (int i = 0; i < N; i++) {
    cnt += (A[i] - 1) / now;
  }
  if (cnt <= K)
    return true;
  else
    return false;
}

int main() {
  cin >> N >> K;

  for (int i = 0; i < N; i++)
    cin >> A[i];
  ll L = 0, R = 1e18;
  while (R - L > 1) {
    ll now = (L + R) / 2;
    if (Solve(now))
      R = now;
    else
      L = now;
  }
  cout << R << endl;

  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error


#include <bits/stdc++.h>

#define FAST_INPUT                                                             \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define ll long long
using namespace std;

int n, k;
vector<int> nums;

bool ok(int max_size) {
  ll total_cuts = 0;
  for (int i = 0; i < n; i++) {
    int val = nums[i];
    total_cuts += (val / max_size + (val % max_size != 0) - 1);
  }
  return total_cuts <= k;
}

void solve() {
  cin >> n >> k;
  nums = vector<int>(n);
  int mx = 0;
  for (int i = 0; i < n; i++) {
    cin >> nums[i];
    mx = max(mx, nums[i]);
  }

  int l = 1, r = mx;
  int ans = INT_MAX;
  while (l <= r) {
    int mid = l + (r - l) / 2;

    if (ok(mid)) {
      ans = min(ans, mid);
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }

  cout << ans << endl;
}

int main() {
  FAST_INPUT;

  solve();

  return 0;
}


Error: 0

Fixed submission:


#include <bits/stdc++.h>

#define FAST_INPUT                                                             \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define ll long long
using namespace std;

int n, k;
vector<int> nums;

bool ok(int max_size) {
  ll total_cuts = 0;
  for (int i = 0; i < n; i++) {
    total_cuts += nums[i] / max_size;
    if (total_cuts > k)
      return false;
  }
  return total_cuts <= k;
}

void solve() {
  cin >> n >> k;
  nums = vector<int>(n);
  int mx = 0;
  for (int i = 0; i < n; i++) {
    cin >> nums[i];
    mx = max(mx, nums[i]);
  }

  int l = 0, r = mx;
  while (l < r) {
    int mid = l + (r - l) / 2;
    if (ok(mid)) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }

  cout << l << endl;
}

int main() {
  FAST_INPUT;

  solve();

  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
#define ll long long
using namespace std;
int n, k;
ll ans;
vector<ll> logs;
void binary(ll l, ll r) {
  if (l > r)
    return;
  ll m = (l + r) / 2, ck = k;
  for (int i = n - 1; i >= 0; i--) {
    if (logs[i] <= m)
      break;
    ck -= (logs[i] / m - 1);
    if (logs[i] % m)
      ck--;
  }
  if (ck < 0) {
    binary(m + 1, r);
  } else {
    ans = min(ans, m);
    binary(l, m - 1);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> k;
  logs = vector<ll>(n);
  for (int i = 0; i < n; i++) {
    cin >> logs[i];
  }
  sort(logs.begin(), logs.end());
  ans = logs.back();
  binary(1, logs.back());
  cout << ans;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void binary(vector<int> &logs, const int k, int l, int r, int &ans) {
  if (l > r)
    return;
  int m = (l + r) / 2, ck = k;
  for (int i = 0; i < logs.size(); i++) {
    if (logs[i] <= m)
      continue;
    ck -= (logs[i] / m - 1);
    if (logs[i] % m)
      ck--;
  }
  if (ck < 0) {
    binary(logs, k, m + 1, r, ans);
  } else {
    ans = min(ans, m);
    binary(logs, k, l, m - 1, ans);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;
  vector<int> logs(n);
  for (int i = 0; i < n; i++) {
    cin >> logs[i];
  }
  sort(logs.begin(), logs.end());
  int ans = logs.back();
  binary(logs, k, 0, logs.back(), ans);
  cout << ans;
}