답안 #101836

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
101836 2019-03-20T12:43:03 Z cerberus Gap (APIO16_gap) C++17
100 / 100
65 ms 1448 KB
/* cerberus97 - Hanit Banga */

#include <iostream>
#include <iomanip>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>

#ifndef LOCAL
#include "gap.h"
#endif

using namespace std;

#define pb push_back
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL)

typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

const ll INF = 1e18;

ll findGap(int T, int N);
ll findGap_small(int N);
void query(ll s, ll t, ll &mn, ll &mx, int T);

int M;
set<pll> S;

#ifdef LOCAL
int main() {
	int T, n; cin >> T >> n;
	vector<ll> v(n);
	for (int i = 1; i <= n; ++i) {
		cin >> v[i - 1];
	}
	sort(v.begin(), v.end());
	for (int i = 0; i < n; ++i) {
		S.insert({v[i], i});
	}
	S.insert({-1, -1});
	S.insert({INF + 1, n});
	cout << findGap(T, n) << endl << M << ' ';
	if (T == 1) {
		cout << (n + 1) / 2 << endl;
	} else {
		cout << 3 * n << endl;
	}
}
#endif

ll findGap(int T, int N) {
	if (T == 1) {
		return findGap_small(N);
	}

	ll mn, mx;
	query(0, INF, mn, mx, T);
	if (N <= 2) {
		return mx - mn;
	}

	ll A = mn, B = mx, best = (B - A + N - 2) / (N - 1), sz = best + 1;
	while (A < B) {
		if (A + sz >= B) {
			query(A + 1, B - 1, mn, mx, T);
			mx = B;
			if (mn == -1) {
				mn = B;
			}
		} else {
			query(A + 1, A + sz, mn, mx, T);
		}
		if (mn == -1) {
			sz *= 2;
			continue;
		}
		best = max(best, mn - A);
		sz = best + 1;
		A = mx;
	}
	return best;
}

ll findGap_small(int N) {
	ll mn, mx;
	query(0, INF, mn, mx, 1);
	ll best = 0;
	ll A = mn, B = mx;
	N -= 2;
	while (A < B and N > 0) {
		query(A + 1, B - 1, mn, mx, 1);
		if (mn == -1) {
			best = max(best, B - A);
			break;
		} else {
			best = max(best, mn - A);
			best = max(best, B - mx);
			A = mn; B = mx;
			N -= 2;
		}
	}
	return max(best, B - A);
}

void query(ll s, ll t, ll &mn, ll &mx, int T) {
	if (s > t) {
		mn = mx = -1;
		return;
	}
#ifdef LOCAL
	assert(s <= t);
	++M;
	auto l = S.lower_bound({s, -INF});
	auto r = S.upper_bound({t, INF + 10});
	--r;
	if (l->second <= r->second) {
		mn = l->first;
		mx = r->first;
		if (T == 2) {
			M += (r->second - l->second + 1);
		}
	} else {
		mn = mx = -1;
	}
#else
	MinMax(s, t, &mn, &mx);
#endif
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 256 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 3 ms 384 KB Output is correct
15 Correct 3 ms 384 KB Output is correct
16 Correct 15 ms 640 KB Output is correct
17 Correct 14 ms 640 KB Output is correct
18 Correct 15 ms 812 KB Output is correct
19 Correct 15 ms 640 KB Output is correct
20 Correct 15 ms 640 KB Output is correct
21 Correct 55 ms 1400 KB Output is correct
22 Correct 46 ms 1272 KB Output is correct
23 Correct 47 ms 1272 KB Output is correct
24 Correct 46 ms 1280 KB Output is correct
25 Correct 41 ms 1388 KB Output is correct
26 Correct 48 ms 1280 KB Output is correct
27 Correct 57 ms 1448 KB Output is correct
28 Correct 49 ms 1276 KB Output is correct
29 Correct 50 ms 1276 KB Output is correct
30 Correct 47 ms 1308 KB Output is correct
31 Correct 2 ms 384 KB Output is correct
32 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 3 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 12 ms 768 KB Output is correct
17 Correct 12 ms 768 KB Output is correct
18 Correct 11 ms 768 KB Output is correct
19 Correct 11 ms 748 KB Output is correct
20 Correct 5 ms 640 KB Output is correct
21 Correct 34 ms 1276 KB Output is correct
22 Correct 41 ms 1272 KB Output is correct
23 Correct 41 ms 1272 KB Output is correct
24 Correct 36 ms 1272 KB Output is correct
25 Correct 65 ms 1272 KB Output is correct
26 Correct 35 ms 1304 KB Output is correct
27 Correct 48 ms 1348 KB Output is correct
28 Correct 37 ms 1272 KB Output is correct
29 Correct 48 ms 1272 KB Output is correct
30 Correct 17 ms 1272 KB Output is correct
31 Correct 2 ms 384 KB Output is correct
32 Correct 2 ms 384 KB Output is correct