Problem p02598 (C++) — buggy submission, verdict: Runtime Error


#include <bits/stdc++.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

using namespace std;
#define M 1000000001
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define repb(i, a, b) for (int i = a; i >= b; --i)
#define repv(arr) for (auto it = arr[i].begin(); it != arr[i].end(); ++it)
#define vi vector<int>
#define vb vector<bool>
#define vs vector<string>
#define vl vector<long long int>
#define vc vector<char>
#define sz(a) (int)a.size()
#define ssortA(arr) stable_sort(arr.begin(), arr.end())
#define ssortB(arr) stable_sort(arr.begin(), arr.end(), greater<int>());
#define pii pair<int, int>
#define pli pair<long long, int>
#define pll pair<long long, long long>
#define fi first
#define se second
#define search(arr, c) binary_search(arr.begin(), arr.end(), c)
#define pb push_back
#define ll long long
#define mp make_pair
#define endl "\\n"
#define LCM(a, b) boost::math::lcm(a, b)
#define cl_bf cin.ignore(numeric_limits<streamsize>::max(), '\\n');
#define check(ds, x) (int)(ds.find(x) != ds.end() ? 1 : 0)
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll n, k;
  cin >> n >> k;
  vl arr(n);
  rep(i, 0, n) { cin >> arr[i]; }
  ll high = M;
  ll low = 1, mark = M;
  ll mid;
  while (high >= low) {
    mid = (high + low) / 2;
    // if(high<=13)
    // cout<<low<<" "<<high<<endl;
    int flag = 0;
    ll count = 0;
    rep(i, 0, n) {
      if (arr[i] <= mid) {
        continue;
      }
      if (arr[i] % mid == 0) {
        count += arr[i] / mid - 1;
      } else {
        ll x = ceil((double(arr[i])) / mid) - 1;
        /*if(mid==3){
            cout<<x<<" ";
        }*/
        count += x;
      }
    }
    /*if(mid==3){
        cout<<count<<endl;
    }*/
    if (count <= k) {
      mark = mid;
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }
  cout << mark << endl;
}


Error: 0

Fixed submission:


#include <bits/stdc++.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

using namespace std;
#define M 1000000001
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define repb(i, a, b) for (int i = a; i >= b; --i)
#define repv(arr) for (auto it = arr[i].begin(); it != arr[i].end(); ++it)
#define vi vector<int>
#define vb vector<bool>
#define vs vector<string>
#define vl vector<long long int>
#define vc vector<char>
#define sz(a) (int)a.size()
#define ssortA(arr) stable_sort(arr.begin(), arr.end())
#define ssortB(arr) stable_sort(arr.begin(), arr.end(), greater<int>());
#define pii pair<int, int>
#define pli pair<long long, int>
#define pll pair<long long, long long>
#define fi first
#define se second
#define search(arr, c) binary_search(arr.begin(), arr.end(), c)
#define pb push_back
#define ll long long
#define mp make_pair
#define endl "\\n"
#define LCM(a, b) boost::math::lcm(a, b)
#define cl_bf cin.ignore(numeric_limits<streamsize>::max(), '\\n');
#define check(ds, x) (int)(ds.find(x) != ds.end() ? 1 : 0)
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll n, k;
  cin >> n >> k;
  vl arr(n);
  rep(i, 0, n) { cin >> arr[i]; }
  ll high = M;
  ll low = 0, mark = M;
  ll mid;
  while (high >= low) {
    mid = (high + low) / 2;
    // if(high<=13)
    // cout<<low<<" "<<high<<endl;
    int flag = 0;
    ll count = 0;
    rep(i, 0, n) {
      if (arr[i] <= mid) {
        continue;
      }
      if (arr[i] % mid == 0) {
        count += arr[i] / mid - 1;
      } else {
        ll x = ceil((double(arr[i])) / mid) - 1;
        /*if(mid==3){
            cout<<x<<" ";
        }*/
        count += x;
      }
    }
    /*if(mid==3){
        cout<<count<<endl;
    }*/
    if (count <= k) {
      mark = mid;
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }
  cout << mark << endl;
}