alexjercan/bugnet huggingface dataset:
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;

int main(void) {
  int n;
  cin >> n;
  string c;
  cin >> c;
  int cnt = 0, l = 0, r = n - 1;
  while (1) {
    while (l < n && c[l] != 'W')
      ++l;
    while (r >= 0 && c[r] != 'R')
      --r;
    if (l < r) {
      ++cnt;
      c[l] = 'R';
      c[r] = 'w';
    } else
      break;
  }
  cout << cnt << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;

int main(void) {
  int n;
  cin >> n;
  string c;
  cin >> c;
  int cnt = 0, l = 0, r = n - 1;
  while (1) {
    while (c[l] != 'W')
      ++l;
    while (c[r] != 'R')
      --r;
    if (l < r) {
      ++cnt;
      c[l] = 'R';
      c[r] = 'w';
      ++l;
      --r;
    } else
      break;
  }
  cout << cnt << endl;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++) // 0-indexed昇順

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  ll res = 0;
  int L = 0, R = N - 1;
  while (1) {
    for (; L < N; L++)
      if (S[L] == 'W')
        break;
    for (; R >= 0; R--)
      if (S[R] == 'R')
        break;
    if (L > R)
      break;
    res++;
    swap(S[L], S[R]);
  }
  cout << res << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++) // 0-indexed昇順

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  ll res = 0;
  int L, R;
  while (1) {
    for (L = 0; L < N; L++)
      if (S[L] == 'W')
        break;
    for (R = N - 1; R >= 0; R--)
      if (S[R] == 'R')
        break;
    if (L > R)
      break;
    res++;
    swap(S[L], S[R]);
  }
  cout << res << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>

#include <array>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

#define PI 3.14159265358979323846
#define int64 long long
#define uint64 unsigned long long
#define coutfix(i)                                                             \\
  cout << fixed << setprecision(i) // coutの浮動小数出力の桁数設定
using namespace std;

int main() {
  int64 nn, rr = 0, ww = 0, ans = 0, memo = 0;
  char tmp;
  cin >> nn;
  vector<int64> cc(nn);
  for (int64 ii = 0; ii < nn; ii++) {
    cin >> tmp;
    if (tmp == 'R') {
      cc[ii] = 1;
      rr++;
    } else {
      cc[ii] = 0;
      ww++;
    }
  }
  for (int64 ii = 0; ii < rr; ii++) {
    if (cc[ii] == 0) {
      ans++;
    }
  }
  for (int64 ii = rr; ii < nn; ii++) {
    if (cc[ii] == 1) {
      memo++;
    }
  }
  ans = min(ans, memo);
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>

#include <array>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

#define PI 3.14159265358979323846
#define int64 long long
#define uint64 unsigned long long
#define coutfix(i)                                                             \\
  cout << fixed << setprecision(i) // coutの浮動小数出力の桁数設定
using namespace std;

int main() {
  int64 nn, rr = 0, ww = 0, ans = 0;
  char tmp;
  cin >> nn;
  vector<int64> cc(nn);
  for (int64 ii = 0; ii < nn; ii++) {
    cin >> tmp;
    if (tmp == 'R') {
      cc[ii] = 1;
      rr++;
    } else {
      cc[ii] = 0;
      ww++;
    }
  }
  for (int64 ii = 0; ii < nn; ii++) {
    if (cc[ii] == 0) {
      for (int64 jj = nn - 1; jj >= ii; jj--) {
        if (cc[jj] == 1) {
          cc[jj] = 0;
          ans++;
          break;
        }
        if (ii == jj) {
          cout << ans << endl;
          return 0;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)

void solve() {
  int N;
  cin >> N;
  string st;
  cin >> st;

  int i, j;
  int step = 0;
  i = 1;
  j = 2;
  while (i < j) {
    int i = step;
    int j = N - step - 1;

    while (st[i] != 'W' && i < N)
      i++;
    while (st[j] != 'R' && j >= 0)
      j--;

    if (i > j)
      break;
    char tmp = st[i];
    st[i] = st[j];
    st[j] = tmp;
    step++;
  }
  cout << step << endl;
}

int main() {
  solve();
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)

void solve() {
  int N;
  cin >> N;
  string st;
  cin >> st;
  // cout << st << N;
  int i, j;
  int step = 0;
  i = 1;
  j = 2;
  while (i < j) {
    int i = step;
    int j = N - step - 1;
    // cout << "i: " << i << " j: " << j << endl;
    while (st[i] != 'W')
      i++;
    while (st[j] != 'R')
      j--;
    // cout << "i: " << i << " j: " << j << endl;

    if (i > j)
      break;
    char tmp = st[i];
    st[i] = st[j];
    st[j] = tmp;
    step++;
  }
  cout << step << endl;
}

int main() {
  solve();
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(a, b) for (int i = a; i < b; i++)
using namespace std;

void troca(int i, int j, char *vet) {
  char aux;
  aux = vet[i];
  vet[i] = vet[j];
  vet[j] = aux;
}

int main() {
  int x, res = 0, sim1 = 0, sim2 = 0, parei, para = 0;
  char aux[212345];
  scanf(" %d", &x);
  scanf(" %[^\\n]", aux);
  rep(0, x) {
    if (aux[i] == 'W')
      sim1 = 1;
    if (aux[i] == 'R')
      sim2 = 1;
  }
  if (sim1 and sim2) {
    parei = x - 1;
    for (int i = 0; i <= x; i++) {
      // printf("I = %d\\n", i);
      para = 0;
      if (aux[i] == 'W') {
        for (int j = parei; j >= i; j--) {
          // printf("J = %d\\n", j);
          if (aux[j] == 'R') {
            parei = j - 1;
            troca(i, j, aux);
            res++;
            para = 1;
            break;
          }
        }
        if (!para) {
          break;
        }
      }
    }
  }
  // printf("%s\\n", aux);
  printf("%d\\n", res);

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(a, b) for (int i = a; i < b; i++)
using namespace std;

void troca(int i, int j, char *vet) {
  char aux;
  aux = vet[i];
  vet[i] = vet[j];
  vet[j] = aux;
}

int main() {
  int x, res = 0, sim1 = 0, sim2 = 0, parei;
  char aux[212345];
  scanf(" %d", &x);
  scanf(" %[^\\n]", aux);
  rep(0, x) {
    if (aux[i] == 'W')
      sim1 = 1;
    if (aux[i] == 'R')
      sim2 = 1;
  }
  if (sim1 and sim2) {
    parei = x - 1;
    for (int i = 0; i <= x; i++) {
      // printf("I = %d\\n", i);
      if (aux[i] == 'W') {
        for (int j = parei; j >= i; j--) {
          // printf("J = %d\\n", j);
          if (aux[j] == 'R') {
            parei = j - 1;
            troca(i, j, aux);
            res++;
            break;
          }
        }
      }
    }
  }
  // printf("%s\\n", aux);
  printf("%d\\n", res);

  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

int main(int argc, char *argv[]) {
  int N;
  std::cin >> N;
  std::vector<char> v;

  char t;
  for (int i = 0; i < N; i++) {
    std::cin >> t;
    v.push_back(t);
  }

  // for(int i=0; i<N; i++) {
  //     std::cout << v[i];
  // }

  int min = N + 1;
  int p = 0, q = 0;
  for (int i = 0; i < N; i++) {
    if (v[i] == 'R') {
      q++;
    }
  }
  min = q;

  for (int b = 1; b < v.size() + 1; b++) {
    // for(int i=0; i<N; i++) {
    //     if(i<b && v[i]=='W') {
    //         p++;
    //     }
    //     if(i>=b && v[i]=='R') {
    //         q++;
    //     }
    // }
    if (v[b - 1] == 'W') {
      p++;
    }
    if (v[b - 1] == 'R') {
      q--;
    }
    // std::cout << "b=" << b << "pair(" << p << ", " << q << ")" << std::endl;
    int ans;
    if (p > q) {
      ans = q + (p - q);
    } else {
      ans = p + (q - p);
    }
    if (ans < min)
      min = ans;
  }
  std::cout << min;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

int main(int argc, char *argv[]) {
  int N;
  std::cin >> N;
  std::vector<char> v;

  char t;
  for (int i = 0; i < N; i++) {
    std::cin >> t;
    v.push_back(t);
  }

  // for(int i=0; i<N; i++) {
  //     std::cout << v[i];
  // }

  int min = N + 1;
  for (int b = 0; b < v.size() + 1; b++) {
    int p = 0, q = 0;
    for (int i = 0; i < N; i++) {
      if (i < b && v[i] == 'W') {
        p++;
      }
      if (i >= b && v[i] == 'R') {
        q++;
      }
    }
    // std::cout << "b=" << b << "pair(" << p << ", " << q << ")" << std::endl;
    int ans;
    if (p > q) {
      ans = q + (p - q);
    } else {
      ans = p + (q - p);
    }
    if (ans < min)
      min = ans;
  }
  std::cout << min;

  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  int count = 0;
  int left = 0, right = N - 1;
  while (left < right) {
    if (S[left] == 'W' && S[right] == 'R') {
      count++;
      left++;
      right--;
    } else if (S[left] == 'W') {
      right--;
    } else if (S[right] == 'R') {
      left++;
    } else {
      left++;
      right--;
    }
  }
  cout << count << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  int count = 0;
  int left = 0, right = N - 1;
  while (left < right) {
    if (S[left] == 'W' && S[right] == 'R') {
      count++;
      left++;
      right--;
    } else if (S[left] == 'W') {
      right--;
    } else if (S[right] == 'R') {
      left++;
    }
  }
  cout << count << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
typedef long long ll;
typedef unsigned long long ull;

vector<int> w, r;
int main() {
  int n;
  string str;
  scanf("%d", &n);
  cin >> str;
  for (int i = 0; i < n; i++) {
    if (str[i] == 'W')
      w.pb(i);
    else if (str[i] == 'R')
      r.pb(i);
  }

  if (r.size() == 0) {
    printf("0");
    return 0;
  }
  reverse(r.begin(), r.end());

  int t = 0, flag = 1;
  ll ans = 0;
  for (auto x : w) {
    if (x < r[t] && t < r.size() - 1) {
      ans++;
      t++;
    } else if (x < r[t] && t == r.size() - 1 && flag)
      ans++, flag = 0;
  }
  printf("%lld", ans);
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
typedef long long ll;
typedef unsigned long long ull;

vector<int> w, r;
int main() {
  int n;
  string str;
  scanf("%d", &n);
  cin >> str;
  for (int i = 0; i < n; i++) {
    if (str[i] == 'W')
      w.pb(i);
    else if (str[i] == 'R')
      r.pb(i);
  }
  reverse(r.begin(), r.end());
  int t = 0, ans = 0, flag = 1;
  for (auto x : w) {
    if (x < r[t] && t < r.size() - 1) {
      ans++;
      t++;
    } else if (x < r[t] && t == r.size() - 1 && flag)
      ans++, flag = 0;
  }
  printf("%d", ans);
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define DEBUG if (0)
#define lli long long int
#define ldouble long double
using namespace std;

const int maxN = 2e5;
int n;
const int inf = 1e6;
char c[maxN + 1];

// unordered_map<int, int> memo[maxN];
int memo[maxN][2];
int solve(int i = 0, int whites = 0, bool valid = true) {
  if (i == n)
    return valid ? 0 : inf;
  if (memo[i][whites > 0] != -1)
    return memo[i][whites > 0];
  // if (memo[i].count(whites)) memo[i][whites];

  int ans = 0;
  if (c[i] == 'R' && whites) {
    ans = min(whites + solve(i + 1, 0, valid),
              1 + solve(i + 1, whites + 1, valid));
  } else {
    if (c[i] == 'R')
      ans = solve(i + 1, whites, valid);
    else
      ans = solve(i + 1, whites + 1, valid);
  }
  return memo[i][whites > 0] = ans;
  // return memo[i][whites] = ans;
}

int main() {
  while (scanf("%d", &n) != EOF) {
    // for (int i = 0; i < n; i++) memo[i].clear();
    memset(memo, -1, sizeof(memo));

    scanf(" %s", c);
    int ans = solve();

    int w = 0, r = 0;
    for (int i = 0; i < n; i++)
      if (c[i] == 'W')
        w++;
      else
        r++;

    int redToLeft = r;
    for (int i = 0; i < r; i++)
      redToLeft -= c[i] == 'R';
    int whiteToRight = w;
    for (int i = n - 1, j = 0; j < w; i--, j++)
      whiteToRight -= c[i] == 'W';
    ans = min({ans, redToLeft, whiteToRight});

    printf("%d\\n", ans);
  }
  return 0;
}
// WRWWRWRR
// RRRRWWWW
//

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define DEBUG if (0)
#define lli long long int
#define ldouble long double
using namespace std;

const int maxN = 2e5;
int n;
const int inf = 1e6;
char c[maxN + 1];

unordered_map<int, int> memo[maxN];
int solve(int i = 0, int whites = 0, bool valid = true) {
  if (i == n)
    return valid ? 0 : inf;
  // if (memo[i][whites > 0] != -1) return memo[i][whites > 0];
  if (memo[i].count(whites))
    memo[i][whites];

  int ans = 0;
  if (c[i] == 'R' && whites) {
    ans = min(1 + solve(i + 1, whites + 1, valid),
              whites + solve(i + 1, 0, valid));
  } else {
    if (c[i] == 'R')
      ans = solve(i + 1, whites, valid);
    else
      ans = solve(i + 1, whites + 1, valid);
  }
  // return memo[i][whites > 0] = ans;
  return memo[i][whites] = ans;
}

int main() {
  while (scanf("%d", &n) != EOF) {
    for (int i = 0; i < n; i++)
      memo[i].clear();

    scanf(" %s", c);
    int ans = solve();

    int w = 0, r = 0;
    for (int i = 0; i < n; i++)
      if (c[i] == 'W')
        w++;
      else
        r++;

    int redToLeft = r;
    for (int i = 0; i < r; i++)
      redToLeft -= c[i] == 'R';
    int whiteToRight = w;
    for (int i = n - 1, j = 0; j < w; i--, j++)
      whiteToRight -= c[i] == 'W';
    ans = min({ans, redToLeft, whiteToRight});

    printf("%d\\n", ans);
  }
  return 0;
}
// WRWWRWRR
// RRRRWWWW
//
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++) // 0-indexed昇順

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  int res = 0;
  int red = 0;
  vector<int> R;
  vector<int> W;
  rep(i, S.size()) {
    if (S[i] == 'R') {
      red++;
      R.emplace_back(i);
    }
    W.emplace_back(i);
  }
  int j = 0;
  for (auto i : R) {
    if (i < red)
      continue;
    int w = W[j];
    swap(S[j], S[i]);
    res++;
  }
  cout << res << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++) // 0-indexed昇順

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  int res = 0;
  int red = 0;
  rep(i, S.size()) {
    if (S[i] == 'R')
      red++;
  }
  for (int i = 0; i < red; i++) {
    if (S[i] == 'W') {
      for (int j = red; j < S.size(); j++) {
        if (S[j] == 'R') {
          swap(S[j], S[i]);
          res++;
          break;
        }
      }
    }
  }
  cout << res << endl;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
#define sint(a) scanf("%d", &a)
#define sint2(a, b) scanf("%d %d", &a, &b)
#define sll(a) scanf("%lld", &a)
#define sll2(a, b) scanf("%lld %lld", &a, &b)
#define mem(a, i) memset(a, i, sizeof(a))
#define pb push_back
#define int long long
#define ll long long
#define lson node << 1
#define rson (node << 1) + 1
#define endl "\\n"

const int maxn = 2e6 + 10;
const ll mod = 1e9 + 7;
const double pi = acos(-1);

ll qpow(ll a, ll n) {
  ll b = 1;
  while (n) {
    if (n & 1)
      b = b * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return b;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a * b / gcd(a, b); }
// int prime[maxn],s,v[maxn];
// void prime_init()
//{
//     for(int i=2; i<=maxn; i++)
//     {
//         if(!v[i])
//         {
//             prime[s++]=i;
//             for(int j=1; j*i<=maxn; j++)
//                 v[i*j]=1;
//         }
//     }
// }
int n, k;
int a[maxn];
int check(int x) {
  int sum = 0;
  for (int i = 0; i < n; i++) {
    sum += a[i] / x;
    if (a[i] % x == 0)
      sum--;
    if (sum > k)
      return 0;
  }
  return 1;
}
signed main() {
  sll2(n, k);
  int l = 1, r = 0;
  for (int i = 0; i < n; i++) {
    sll(a[i]);
    r = max(r, a[i]);
  }
  r++;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  printf("%lld", r);
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define sint(a) scanf("%d", &a)
#define sint2(a, b) scanf("%d %d", &a, &b)
#define sll(a) scanf("%lld", &a)
#define sll2(a, b) scanf("%lld %lld", &a, &b)
#define mem(a, i) memset(a, i, sizeof(a))
#define pb push_back
#define int long long
#define ll long long
#define lson node << 1
#define rson (node << 1) + 1
#define endl "\\n"

const int maxn = 2e6 + 10;
const ll mod = 1e9 + 7;
const double pi = acos(-1);

ll qpow(ll a, ll n) {
  ll b = 1;
  while (n) {
    if (n & 1)
      b = b * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return b;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a * b / gcd(a, b); }
// int prime[maxn],s,v[maxn];
// void prime_init()
//{
//     for(int i=2; i<=maxn; i++)
//     {
//         if(!v[i])
//         {
//             prime[s++]=i;
//             for(int j=1; j*i<=maxn; j++)
//                 v[i*j]=1;
//         }
//     }
// }
int n, k;
int a[maxn];
int check(int x) {
  int sum = 0;
  for (int i = 0; i < n; i++) {
    sum += a[i] / x;
    if (a[i] % x == 0)
      sum--;
    if (sum > k)
      return 0;
  }
  return 1;
}
signed main() {
  sll2(n, k);
  int l = 0, r = 0;
  for (int i = 0; i < n; i++) {
    sll(a[i]);
    r = max(r, a[i]);
  }
  r++;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  printf("%lld", r);
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define init(a, i)                                                             \\
  for (int k = 0; k < (i); k++)                                                \\
  (a)[k] = 0
#define in(a, i)                                                               \\
  for (int k = 0; k < (i); k++)                                                \\
  cin >> (a)[k]
#define all(a) (a).begin(), (a).end()
#define el(a) (a).end() - (a).begin()
#define mod 1000000007
#define inf 2147483647
#define range(x, a, b) (a) <= x &&x <= (b)
int n, k;
int a[200000];
bool can(int length) {
  ll WorkCount = 0;
  rep(i, n) { WorkCount += (a[i] + (length - 1)) / length - 1; }
  if (WorkCount > k)
    return 0;
  else
    return 1;
}

int main() {
  cin >> n >> k;
  in(a, n);
  int left = 0, right = 1000000000; // 2分探索(left<anser<=right)
  int middle;
  while (right - left != 1) {
    middle = (left + right) / 2;
    if (can(middle)) {
      right = middle;
    } else {
      left = middle;
    }
  }
  cout << right;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define init(a, i)                                                             \\
  for (int k = 0; k < (i); k++)                                                \\
  (a)[k] = 0
#define in(a, i)                                                               \\
  for (int k = 0; k < (i); k++)                                                \\
  cin >> (a)[k]
#define all(a) (a).begin(), (a).end()
#define el(a) (a).end() - (a).begin()
#define mod 1000000007
#define inf 2147483647
#define range(x, a, b) (a) <= x &&x <= (b)
int n, k;
int a[200000];
bool can(int length) {
  ll WorkCount = 0;
  rep(i, n) { WorkCount += (a[i] + (length - 1)) / length - 1; }
  if (WorkCount > k)
    return 0;
  else
    return 1;
}

int main() {
  cin >> n >> k;
  in(a, n);
  int left = -1, right = 1000000000; // 2分探索(left<anser<=right)
  int middle;
  while (right - left != 1) {
    middle = (left + right) / 2;
    if (can(middle)) {
      right = middle;
    } else {
      left = middle;
    }
  }
  cout << right;
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

// By: Luogu@rui_er(122461)
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;

int n, k, l = 1, r, ans;
int a[N];

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

int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    r = max(r, a[i]);
    ans = max(ans, a[i]);
  }
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (f(mid)) {
      ans = mid;
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  printf("%d\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

// By: Luogu@rui_er(122461)
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;

int n, k, l, r, ans;
int a[N];

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

int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    l = min(l, a[i]);
    r = max(r, a[i]);
    ans = max(ans, a[i]);
  }
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (f(mid)) {
      ans = mid;
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
using namespace std;
int n, K, a[200005], mx = 0;
bool Judge(double mid) {
  long long cnt = 0;
  for (int i = 1; i <= n; i++)
    cnt += ceil(a[i] / mid) - 1;
  return cnt <= K;
}
int main() {
  cin >> n >> K;
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), mx = max(mx, a[i]);
  double l = 0, r = mx;
  while (r - l > 1e-6) {
    double mid = (l + r) / 2;
    if (Judge(mid))
      r = mid;
    else
      l = mid;
  }
  printf("%.0lf\\n", ceil((l + r) / 2));
  return 0;
}


Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
using namespace std;
int n, K, a[200005], mx = 0;
bool Judge(double mid) {
  long long cnt = 0;
  for (int i = 1; i <= n; i++)
    cnt += ceil(a[i] / mid) - 1;
  return cnt <= K;
}
int main() {
  cin >> n >> K;
  for (int i = 1; i <= n; i++)
    cin >> a[i], mx = max(mx, a[i]);
  double l = 0, r = mx;
  while (r - l > 1e-7) {
    double mid = (l + r) / 2;
    if (Judge(mid))
      r = mid;
    else
      l = mid;
  }
  printf("%.0lf\\n", ceil((l + r) / 2));
  return 0;
}