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


#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define FOR(x, y) for (ll i = x; i <= y; i++)
#define pb(x) push_back(x)
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vii vector<int>
#define vll vector<ll>
#define matrix(x) vector<vector<x>>
#define vss vector<string>
#define PI (2 * acos(0.0))
#define sqr(x) ((x) * (x))
#define sf(a) scanf("%d", &a)
#define sfl(a) scanf("%lld", &a)
#define sff(a, b) scanf("%d %d", &a, &b)
#define sffl(a, b) scanf("%lld %lld", &a, &b)
#define sfff(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define sfffl(a, b, c) scanf("%lld %lld %lld", &a, &b, &c)
#define ms(a, b) memset(a, b, sizeof(a))
#define ff first
#define ss second
#define lcm(a, b) ((a) * ((b) / __gcd(a, b)))
#define all(a) a.begin(), a.end()
#define readfile freopen("input.txt", "r", stdin);
#define writefile freopen("output.txt", "w", stdout);
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define gap " "
#define mx 104
#define inf (ll)1e9
#define WHITE 1
#define GRAY 2
#define BLACK 3
#define EMPTY_VALUE -1
#define mod 1000000007
#define MOD(a, b) (a % b + b) % b

using namespace std;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

ll x, k;
vll a;

bool possible(ll mid) {
  if (mid <= 0)
    return false;
  ll cnt = 0;
  for (ll i = 0; i < x; i++) {
    if (a[i] <= mid)
      continue;
    // cnt += ceil(a[i]/(mid+0.0))-1;
    cnt += (a[i] - 1) / mid;
  }
  return cnt <= k;
}

int main() {
  // fastio
  sffl(x, k);
  a.resize(x + 1);
  for (ll i = 0; i < x; i++)
    sfl(a[i]);
  ll mini = 0, maxi = 1e9 + 100;

  while (mini < maxi) {
    ll mid = (mini + maxi) / 2;
    // cout<<mid<<endl;
    if (possible(mid)) {
      maxi = mid;
    } else {
      mini = mid + 1;
    }
  }
  cout << mini << endl;

  return 0;
}

Error: 0

Fixed submission:


#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define FOR(x, y) for (ll i = x; i <= y; i++)
#define pb(x) push_back(x)
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vii vector<int>
#define vll vector<ll>
#define matrix(x) vector<vector<x>>
#define vss vector<string>
#define PI (2 * acos(0.0))
#define sqr(x) ((x) * (x))
#define sf(a) scanf("%d", &a)
#define sfl(a) scanf("%lld", &a)
#define sff(a, b) scanf("%d %d", &a, &b)
#define sffl(a, b) scanf("%lld %lld", &a, &b)
#define sfff(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define sfffl(a, b, c) scanf("%lld %lld %lld", &a, &b, &c)
#define ms(a, b) memset(a, b, sizeof(a))
#define ff first
#define ss second
#define lcm(a, b) ((a) * ((b) / __gcd(a, b)))
#define all(a) a.begin(), a.end()
#define readfile freopen("input.txt", "r", stdin);
#define writefile freopen("output.txt", "w", stdout);
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define gap " "
#define mx 104
#define inf (ll)1e9
#define WHITE 1
#define GRAY 2
#define BLACK 3
#define EMPTY_VALUE -1
#define mod 1000000007
#define MOD(a, b) (a % b + b) % b

using namespace std;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

ll x, k;
vll a;

bool possible(ll mid) {
  ll cnt = 0;
  for (ll i = 0; i < x; i++) {
    if (a[i] <= mid)
      continue;
    // cnt += ceil(a[i]/(mid+0.0))-1;
    cnt += (a[i] - 1) / mid;
  }
  return cnt <= k;
}

int main() {
  // fastio
  sffl(x, k);
  a.resize(x + 1);
  for (ll i = 0; i < x; i++)
    sfl(a[i]);
  ll mini = 0, maxi = 1e9 + 100;

  while (mini < maxi) {
    ll mid = (mini + maxi) / 2;
    // cout<<mid<<endl;
    if (possible(mid)) {
      maxi = mid;
    } else {
      mini = mid + 1;
    }
  }
  cout << mini << endl;

  return 0;
}