Submission #40768

#TimeUsernameProblemLanguageResultExecution timeMemory
40768ssnsarang2023Gap (APIO16_gap)C++14
0 / 100
77 ms1564 KiB
#include <bits/stdc++.h>
#include "gap.h"

using namespace std;

typedef long long ll;

//#define DEBUG

#ifdef DEBUG

int n = 4;
ll a[4] = {2, 3, 6, 8};

void MinMax(ll x, ll y, ll *mn, ll *mx) {
	for (int i = 1; i <= n; ++i) if (a[i] >= x)
	{ (*mn) = a[i]; break; }
	for (int i = n; i >= 1; --i) if (a[i] <= y)
	{ (*mx) = a[i]; break; }
}
#endif

ll findGap(int _t, int _n) {
	ll n = _n;
	ll mn = 1, mx = (ll)1e18;
	MinMax(mn, mx, &mn, &mx);
	ll d = (mx - mn) / n, mod = (mx - mn) % n, pre = mn;
	ll pstep = mn, step = mn + d + - 1 + mod, gap = 0;
	while (1) {
		ll mntmp = -1, mxtmp = -1;
		if (pstep <= min(mx - 1, step)) MinMax(pstep + 1, min(mx - 1, step), &mntmp, &mxtmp);
		if (step >= mx) {
			if (mxtmp != -1) gap = max(gap, mx - mxtmp);
			else gap = max(gap, mx - pre);
			break;
		} else if (mntmp != -1) gap = max(gap, mntmp - pre);
		pre = mxtmp, pstep = step, step += d;
	}
	return gap;
}

#ifdef DEBUG
int main() {
	findGap(2, 4);
	return 0;
}
#endif
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...