This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include "gap.h"
using namespace std;
typedef long long ll;
//#define DEBUG
#ifdef DEBUG
int n = 5;
ll a[5] = {2, 3, 6, 8, 12};
void MinMax(ll x, ll y, ll *mn, ll *mx) {
for (int i = 0; i < n; ++i) if (a[i] >= x)
{ (*mn) = a[i]; break; }
for (int i = n - 1; i >= 0; --i) if (a[i] <= y)
{ (*mx) = a[i]; break; }
if ((*mx) < (*mn)) (*mx) = (*mn) = -1;
}
#endif
ll findGap(int _t, int _n) {
ll n = _n;
ll mn = 1, mx = (ll)1e18;
MinMax(mn, mx, &mn, &mx);
if (_t == 2) {
ll d = (mx - mn) / (n - 1ll) + (ll)((mx - mn) % (n - 1ll) != 0ll), pre = mn;
ll pstep = mn, step = mn + d - 1, gap = 0;
while (1) {
ll mntmp = -1, mxtmp = -1;
MinMax(pstep + 1ll, step, &mntmp, &mxtmp);
if (step >= mx - 1ll) {
if (mxtmp != -1ll) gap = max(gap, mx - mxtmp);
else gap = max(gap, mx - pre);
break;
} else if (mntmp != -1ll) gap = max(gap, mntmp - pre);
pre = max(pre, mxtmp), pstep = step, step += d;
}
return gap;
} else {
vector<ll> a;
a.resize(n);
a[0] = mn, a[n - 1] = mx;
int lo = 0, hi = n - 1;
while (hi - lo > 1) {
MinMax(a[lo] + 1ll, a[hi] - 1ll, &mn, &mx);
++lo, --hi;
a[lo] = mn, a[hi] = mx;
}
ll gap = 0;
for (int i = 0; i < n - 1; ++i) gap = max(gap, a[i + 1] - a[i]);
return gap;
}
return 0ll;
}
#ifdef DEBUG
int main() {
printf("%d", findGap(1, 5));
return 0;
}
#endif
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |