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

#include <bits/stdc++.h>

using namespace std;
int a[200005];
bool pos(int x, int k, int n) {
  if (x <= 0)
    return false;
  int cuts = 0;
  for (int i = 0; i < n; i++) {
    int div = (a[i] - 1) / x;
    cuts += div;
  }
  return (cuts <= k);
}
int main() {
  int n, k;
  cin >> n >> k;
  int lo = 0, high = 0, ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    high = max(high, a[i]);
  }
  ans = high;
  while (lo <= high) {
    int mid = (lo + high) / 2;
    // cout<<lo<<" "<<high<<" "<<mid<<"\\n";
    if (pos(mid, k, n)) {
      ans = mid;
      high = mid - 1;
    } else
      lo = mid + 1;
  }
  cout << ans;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
int a[200005];
bool pos(int x, int k, int n) {
  int cuts = 0;
  for (int i = 0; i < n; i++) {
    int div = a[i] / x;
    cuts += div;
  }
  return (cuts <= k);
}
int main() {
  int n, k;
  cin >> n >> k;
  int lo = 0, high = 0, ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    high = max(high, a[i]);
  }
  ans = high;
  while (lo <= high) {
    int mid = (lo + high) / 2;
    // cout<<lo<<" "<<high<<" "<<mid<<"\\n";
    if (pos(mid, k, n)) {
      ans = mid;
      high = mid - 1;
    } else
      lo = mid + 1;
  }
  cout << ans;
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rup(x, y) (((x) + (y)-1) / (y))
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  int l = 0, r = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    r = max(r, a[i]);
  }

  int m = 0, ans = r;
  while (l <= r) {
    m = (l + r) / 2;
    if (m < 1) {
      ans = 1;
      break;
    }
    int count = 0;
    for (int i = 0; i < n; i++) {
      count += rup(a[i], m) - 1;
    }
    // cout << l << "," << r << "," << m << "," << count << endl;
    if (count <= k) {
      ans = m;
      r = m - 1;
    } else
      l = m + 1;
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rup(x, y) (((x) + (y)-1) / (y))
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  int l = 0, r = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    r = max(r, a[i]);
  }

  int m = 0, ans = r;
  while (l < r) {
    m = (l + r) / 2;
    int count = 0;
    for (int i = 0; i < n; i++) {
      count += rup(a[i], m) - 1;
    }
    if (count > k)
      l = m + 1;
    if (count <= k) {
      ans = m;
      r = m - 1;
    }
  }
  while (1) {
    m--;
    int count = 0;
    for (int i = 0; i < n; i++) {
      count += rup(a[i], m) - 1;
    }
    if (count <= k) {
      ans = m;
      m--;
    } else
      break;
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
// #define DEBUG
#define REP(i, nn) for (int i = 0; i < (int)nn; i++)
#define deb(x) std::cout << #x << " " << x << endl;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
template <typename... T> void read(T &...a) { ((cin >> a), ...); }
template <typename... T> void write(T... a) { ((cout << a << "\\n"), ...); }

template <typename... T> void write_space(T... a) { ((cout << a << " "), ...); }

bool check_if_can_cut(vector<ll> &a, ll max_length, ll max_cut) {
  ll count_cut = 0;
  for (int i = 0; i < (int)a.size(); i++) {
    if (a[i] <= max_length)
      continue;
    ll new_cut = a[i] / max_length;
    if (new_cut > max_cut - count_cut)
      return false;
    count_cut += new_cut;
  }
  return true;
}

void solve() {
  int n;
  ll k;
  cin >> n >> k;
  vector<ll> a(n);
  ll sum = 0;
  ll max_term = 1;
  REP(i, n) {
    cin >> a[i];
    sum += a[i];
    if (a[i] > max_term) {
      max_term = a[i];
    }
  }
  if (k == 0) {
    cout << max_term << "\\n";
    return;
  }
  ll lb = 1;
  ll rb = max_term;
  while (lb + 1 < rb) {
    ll mb = (lb + rb) / 2;
    if (check_if_can_cut(a, mb, k)) {
      rb = mb;
    } else {
      lb = mb + 1;
    }
  }
  if (!check_if_can_cut(a, lb, k)) {
    lb++;
  }
  cout << lb << endl;
}

int main() {
  // making data IO Fast
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);

  solve();

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
// #define DEBUG
#define REP(i, nn) for (int i = 0; i < (int)nn; i++)
#define deb(x) std::cout << #x << " " << x << endl;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
template <typename... T> void read(T &...a) { ((cin >> a), ...); }
template <typename... T> void write(T... a) { ((cout << a << "\\n"), ...); }

template <typename... T> void write_space(T... a) { ((cout << a << " "), ...); }

bool check_if_can_cut(vector<ll> &a, ll max_length, ll max_cut) {
  ll count_cut = 0;
  for (int i = 0; i < (int)a.size(); i++) {
    ll new_cut = a[i] / max_length;
    count_cut += new_cut;
    if (count_cut > max_cut)
      return false;
  }
  return true;
}

void solve() {
  int n;
  ll k;
  cin >> n >> k;
  vector<ll> a(n);
  ll sum = 0;
  ll max_term = 1;
  REP(i, n) {
    cin >> a[i];
    sum += a[i];
    if (a[i] > max_term) {
      max_term = a[i];
    }
  }
  if (k == 0) {
    cout << max_term << "\\n";
    return;
  }
  ll lb = sum / (n + k);
  ll rb = max_term;
  while (lb + 1 < rb) {
    ll mb = (lb + rb) / 2;
    if (check_if_can_cut(a, mb, k)) {
      rb = mb;
    } else {
      lb = mb + 1;
    }
  }
  if (!check_if_can_cut(a, lb, k)) {
    lb++;
  }
  cout << lb << endl;
}

int main() {
  // making data IO Fast
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);

  solve();

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

#include <bits/stdc++.h>

#define F first
#define S second
#define MP make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define LCM(a, b) (a) / __gcd((a), (b)) * (b)
#define CEIL(a, b) (a) / (b) + (((a) % (b)) ? 1 : 0)
#define ln '\\n'

using namespace std;
using LL = long long;
using ldouble = long double;
using P = pair<int, int>;
using LP = pair<LL, LL>;

static const int INF = INT_MAX;
static const LL LINF = LLONG_MAX;
static const int MIN = INT_MIN;
static const LL LMIN = LLONG_MIN;
static const int MOD = 1e9 + 7;
static const int SIZE = 200005;

const int dx[] = {0, -1, 1, 0};
const int dy[] = {-1, 0, 0, 1};

vector<LL> Div(LL n) {
  vector<LL> ret;
  for (LL i = 1; i * i <= n; ++i) {
    if (n % i == 0) {
      ret.pb(i);
      if (i * i != n)
        ret.pb(n / i);
    }
  }
  sort(all(ret));
  return ret;
}

int K, N;
vector<int> v;

bool check(int n) {
  int t = K;
  for (int i = 0; i < N; ++i) {
    t -= v[i] / n;
    if (v[i] >= n && v[i] % n == 0)
      ++t;
  }
  return (t >= 0);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> N >> K;

  int mx = 0;
  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    v.pb(a);
    mx = max(mx, a);
  }

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

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define F first
#define S second
#define MP make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define LCM(a, b) (a) / __gcd((a), (b)) * (b)
#define CEIL(a, b) (a) / (b) + (((a) % (b)) ? 1 : 0)
#define ln '\\n'

using namespace std;
using LL = long long;
using ldouble = long double;
using P = pair<int, int>;
using LP = pair<LL, LL>;

static const int INF = INT_MAX;
static const LL LINF = LLONG_MAX;
static const int MIN = INT_MIN;
static const LL LMIN = LLONG_MIN;
static const int MOD = 1e9 + 7;
static const int SIZE = 200005;

const int dx[] = {0, -1, 1, 0};
const int dy[] = {-1, 0, 0, 1};

vector<LL> Div(LL n) {
  vector<LL> ret;
  for (LL i = 1; i * i <= n; ++i) {
    if (n % i == 0) {
      ret.pb(i);
      if (i * i != n)
        ret.pb(n / i);
    }
  }
  sort(all(ret));
  return ret;
}

int K, N;
vector<int> v;

bool check(int n) {
  int t = K;
  for (int i = 0; i < N; ++i) {
    t -= v[i] / n;
    if (v[i] >= n && v[i] % n == 0)
      ++t;
  }
  return (t >= 0);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> N >> K;

  int mx = 0;
  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    v.pb(a);
    mx = max(mx, a);
  }

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

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

#include <bits/stdc++.h>
#define ll long long int
#define forr(i, n) for (ll i = 0; i < n; i++)
#define mod 1000000007
#define oo 2000000000000000000LL
using namespace std;

ll up(double x) {
  if (x - (ll)x > 0)
    return (ll)x + 1;
  else
    return x;
}

bool check(ll a[], ll n, ll m, ll k) {
  ll tot = 0;
  forr(i, n) {
    if (a[i] > m)
      tot += up((double)a[i] / m) - 1;
  }
  return tot <= k;
}

void _sol() {
  ll k;
  ll n;
  cin >> n >> k;
  ll a[n];
  forr(i, n) cin >> a[i];
  ll s = 1, e = 1e10, ans = -1;
  while (s <= e) {
    ll mid = (s + e) / 2.0;
    if (check(a, n, mid, k))
      ans = mid, e = mid - 1;
    else
      s = mid + 1;
  }
  cout << ans;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  _sol();
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define forr(i, n) for (ll i = 0; i < n; i++)
#define mod 1000000007
#define oo 2000000000000000000LL
using namespace std;

bool check(ll a[], ll n, ll m, ll k) {
  ll tot = 0;
  forr(i, n) tot += a[i] / m;
  return tot <= k;
}

void _sol() {
  ll n, k;
  cin >> n >> k;
  ll a[n];
  forr(i, n) cin >> a[i];
  ll s = 0, e = 1e10, ans = -1;
  while (s <= e) {
    ll mid = (s + e) / 2;
    if (check(a, n, mid, k))
      ans = mid, e = mid - 1;
    else
      s = mid + 1;
  }
  cout << ans;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  _sol();
}
Problem p02598 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const double eps = 1e-5;
const int N = 2e5 + 500;
int w[N], n, K;
int judge(int x) {
  long long cnt = 0;
  for (int i = 1; i <= n && cnt <= K; i++)
    cnt += (w[i] + x - 1) / x - 1;
  return cnt <= K;
}
int main() {
  scanf("%d%d", &n, &K);
  int mx = 0;
  for (int i = 1; i <= n; i++)
    scanf("%d", w + i), mx = max(mx, w[i]);
  int l = 1, r = mx, mid;
  while (l != r) {
    mid = (l + r) >> 1;
    if (judge(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << endl;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const double eps = 1e-9;
const int N = 2e5 + 500;
int w[N], n, K;
int judge(long double x) {
  int cnt = 0;
  for (int i = 1; i <= n; i++)
    cnt += ceil(w[i] / x) - 1;
  return cnt <= K;
}
int main() {
  scanf("%d%d", &n, &K);
  int mx = 0;
  for (int i = 1; i <= n; i++)
    scanf("%d", w + i), mx = max(mx, w[i]);
  double l = 0, r = mx, mid;
  while (r - l > eps) {
    mid = (l + r) / 2;
    if (judge(mid))
      r = mid;
    else
      l = mid;
  }
  printf("%.0lf\\n", ceil((l + r) / 2) + eps);
}
Problem p02598 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define ll long long
#define forr(i, n) for (ll i = 0; i < n; i++)
#define f(i, a, b) for (int i = a; i < b; i++)
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define vi vector<ll>
#define endl '\\n'
#define ce(ele) cout << ele << ' '
#define cs(ele) cout << ele << '\\n'
#define CASE(t)                                                                \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define sor(v) sort(v.begin(), v.end())
#define rev(v) reverse(v.begin(), v.end())

ll power(ll a, ll b) {
  ll r = 1;
  while (b--)
    r *= a;
  return r;
}
const ll INF = 1e17L + 5;
const int mod = 1e9 + 7;
int solve(ll mx, ll n, vector<ll> l, ll k, ll i) {
  ll cnt = 0;
  if (i == 0)
    return 0;
  f(j, 0, n) cnt += (l[j] - 1 + i) / i - 1;
  return cnt <= k ? 1 : 0;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  ll n, k, mx = 0;
  cin >> n >> k;
  vector<ll> a(n);
  f(i, 0, n) {
    cin >> a[i];
    mx = max(mx, a[i]);
  }
  ll l = -1, r = mx;
  while (l < r - 1) {
    ll m = l + (r - l) / 2;
    if (solve(mx, n, a, k, m)) {
      r = m;
    } else
      l = m;
  }
  cs(r);
}


Error: TLE

Fixed submission:

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

#define ll long long
#define forr(i, n) for (ll i = 0; i < n; i++)
#define f(i, a, b) for (int i = a; i < b; i++)
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define vi vector<ll>
#define endl '\\n'
#define ce(ele) cout << ele << ' '
#define cs(ele) cout << ele << '\\n'
#define CASE(t)                                                                \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define sor(v) sort(v.begin(), v.end())
#define rev(v) reverse(v.begin(), v.end())

ll power(ll a, ll b) {
  ll r = 1;
  while (b--)
    r *= a;
  return r;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  ll n, k, mx = 0;
  cin >> n >> k;
  vector<ll> l(n);
  f(i, 0, n) {
    cin >> l[i];
    mx = max(mx, l[i]);
  }
  ll cnt = 0;
  f(i, 1, mx + 1) {
    cnt = 0;
    f(j, 0, n) cnt += (l[j] - 1) / i;
    if (cnt <= k) {
      cs(i);
      return 0;
    }
  }
}
Problem p02598 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N;
  long long K;
  cin >> N >> K;
  priority_queue<long long> que;
  for (int i = 0; i < N; i++) {
    long long A;
    cin >> A;
    que.push(A);
  }

  auto f = [&](double t, priority_queue<long long> q) {
    long long k = 0;
    for (int i = 0; i < N; i++) {
      long long A = q.top();
      q.pop();
      long long n = ceil(A / t);
      k += n - 1;
    }
    return k;
  };
  double l = 0;
  double r = que.top();
  for (int i = 0; i < 100; i++) {
    double m = (l + r) / 2;
    long long k = f(m, que);
    if (k <= K)
      r = m;
    else
      l = m;
  }
  long long ans = ceil(r);

  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  int N;
  long long K;
  cin >> N >> K;
  priority_queue<long long> que;
  for (int i = 0; i < N; i++) {
    long long A;
    cin >> A;
    que.push(A);
  }

  auto f = [&](double t, priority_queue<long long> q) {
    long long k = 0;
    for (int i = 0; i < N; i++) {
      long long A = q.top();
      q.pop();
      long long n = A / t;
      k += n;
    }
    return k;
  };
  double l = 0;
  double r = que.top();
  for (int i = 0; i < 1000; i++) {
    double m = (l + r) / 2;
    long long k = f(m, que);
    if (k <= K)
      r = m;
    else
      l = m;
  }
  long long ans = ceil(r);

  cout << ans << endl;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, x) for (ll i = 0; i < x; i++)
#define rep2(i, x) for (ll i = 1; i <= x; i++)
#define all(a) (a).begin(), (a).end()
using ll = long long;
using ld = long double;
using namespace std;
const ll INF = 1000000000000000000;
const ll mod = 1000000007;
const ld pi = 3.141592653589793238;

ll test[12345678];
signed main() {
  string s, t;
  ll n, m, k, q, a = 0, b = 0, c = 0, d, x, y, ans = 1;
  cin >> n >> k;
  rep(i, n) { cin >> test[i]; }
  ll ok = 0, ng = mod, mid = (ok + ng) / 2;
  while (ng - ok > 1) {
    ll cnt = 0;
    rep(i, n) {
      if (mid != 0) {
        cnt += (test[i] + mid - 1) / mid;
        cnt--;
      }
    }
    if (cnt > k) {
      ok = mid;
    } else {
      ng = mid;
    }
    mid = (ok + ng) / 2;
  }
  cout << ng << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
// #include<bits/stdc++.h>
#define rep(i, x) for (ll i = 0; i < x; i++)
#define rep2(i, x) for (ll i = 1; i <= x; i++)
#define all(a) (a).begin(), (a).end()
using ll = long long;
using ld = long double;
using namespace std;
const ll INF = 1000000000000000000;
const ll mod = 1000000007;
const ld pi = 3.141592653589793238;

/*bool IsPrime(ll num) {
        if (num < 2) return false;
        else if (num == 2) return true;
        else if (num % 2 == 0) return false; // 偶数はあらかじめ除く
        double sqrtNum = sqrt(num);
        for (int i = 3; i <= sqrtNum; i += 2) {
                if (num % i == 0) { // 素数ではない
                        return false;
                }
        }
        // 素数である
        return true;
}*/
/*ll gcd(ll a, ll b) {
        if (a < b)swap(a, b);
        if (a % b == 0)return b;
        return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
        return a / gcd(a, b) * b;
}*/
/*ll modpow(ll x, ll y) {
        ll res = 1;
        while (y) {
                if (y % 2) { res *= x; res %= mod; }
                x = x * x % mod; y /= 2;
        }
        return res;
}*/
/*ll keta(ll n) {
        ll res = 0;
        while (n >= 1) {
                res += n % 10; n /= 10;
        }
        return res;
}*/
/*ll nCk(ll n, ll k) {
        ll a = 1, b = 1;
        for (int h = n - k + 1; h <= n; h++) { a *= h; a %= mod; }
        for (int h = 1; h <= k; h++) { b *= h; b %= mod; }
        return a * modpow(b, mod - 2) % mod;
}*/
// printf("%.10f\\n", n);

ll test[123456];
signed main() {
  string s, t;
  ll n, m, k, q, a = 0, b = 0, c = 0, d, x, y, ans = 1;
  cin >> n >> k;
  rep(i, n) { cin >> test[i]; }
  ll ok = 0, ng = mod, mid = (ok + ng) / 2;
  while (ng - ok > 1) {
    ll cnt = 0;
    rep(i, n) {
      cnt += (test[i] + mid - 1) / mid;
      cnt--;
    }
    if (cnt > k) {
      ok = mid;
    } else {
      ng = mid;
    }
    mid = (ok + ng) / 2;
  }
  cout << ng << endl;
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  int MIN = 0;
  int MAX = a[n - 1];
  int MID = (MAX + MIN) / 2;
  int tmp = 0;
  while (MAX - MIN > 1) {
    MID = (MAX + MIN) / 2;
    tmp = 0;
    for (int i = 0; i < n; i++)
      tmp += ((a[i] + MID - 1) / MID - 1);
    if (tmp > k)
      MIN = MID;
    else
      MAX = MID;
  }
  cout << MAX;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  int MIN = 0;
  int MAX = a[n - 1];
  int MID = (MAX + MIN) / 2;
  int tmp = 0;
  while (MAX - MIN > 1) {
    MID = (MAX + MIN) / 2;
    tmp = 0;
    for (int i = 0; i < n; i++)
      tmp += (a[i] / MID);
    if (tmp > k)
      MIN = MID;
    else
      MAX = MID;
  }
  int tmp_b = 0;
  if (MIN != 0)
    cout << MAX;
  else {
    for (int i = 0; i < n; i++)
      tmp_b += (a[i] / MIN);
    if (tmp_b <= k)
      cout << MIN;
    else
      cout << MAX;
  }
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
#define fr(i, a, b) for (ll i = a; i < b; i++)

int main() {
  ios::sync_with_stdio(false);
#ifdef CODE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ll n, k;
  cin >> n >> k;
  vl arr(n);
  fr(i, 0, n) { cin >> arr[i]; }
  ll low = 1, high = 1e+10;
  ll ans = high;

  auto func = [&](ll x) {
    ll total = 0;
    fr(i, 0, n) {
      int temp = arr[i] / x;
      if (x * temp == arr[i])
        temp--;
      total += temp;
    }
    return (total <= k);
  };
  while (low <= high) {
    ll mid = low + (high - low) / 2;
    if (func(mid)) {
      ans = mid;
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }
  cout << ans;

  return 0;
}

Error: WA

Fixed submission:

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
#define fr(i, a, b) for (ll i = a; i < b; i++)

int main() {
  ios::sync_with_stdio(false);
#ifdef CODE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ll n, k;
  cin >> n >> k;
  vl arr(n);
  fr(i, 0, n) { cin >> arr[i]; }
  ll low = 0, high = *max_element(arr.begin(), arr.end());
  ll ans = high;

  auto func = [&](ll x) {
    ll total = 0;
    fr(i, 0, n) {
      int temp = arr[i] / x;
      if (x * temp == arr[i])
        temp--;
      total += temp;
    }
    return (total <= k);
  };
  while (low <= high) {
    ll mid = low + (high - low) / 2;
    if (func(mid)) {
      ans = mid;
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }
  cout << ans;

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

#include <bits/stdc++.h>
#define endl "\\n"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const long long INF = 1e18;
// const ll mod = 1000000007;

ll N, K;
ll A[201000];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  ll ok = 1e9;
  ll ng = 0;
  while (abs(ok - ng) > 1) {
    ll mid = (ok + ng) / 2;
    ll timer = 0;
    for (int i = 0; i < N; i++) {
      timer += (A[i] - 1) / mid;
    }
    if (timer <= K)
      ok = mid;
    else
      ng = mid;
  }
  cout << ok << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define endl "\\n"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const long long INF = 1e18;
// const ll mod = 1000000007;

ll N, K;
ll A[201000];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  ll ok = 1e9;
  ll ng = 0;
  for (int _ = 0; _ <= 100; _++) {
    ll mid = (ok + ng) / 2;
    ll timer = 0;
    for (int i = 0; i < N; i++) {
      timer += (A[i] - 1) / mid;
    }
    if (timer <= K)
      ok = mid;
    else
      ng = mid;
  }
  cout << ok << endl;
  return 0;
}