Submission #1327678

#TimeUsernameProblemLanguageResultExecution timeMemory
1327678itzmanuWatching (JOI13_watching)C++20
100 / 100
51 ms31932 KiB
#include <bits/stdc++.h>
using namespace std;

#define nitro ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long
#define vi vector<int>
#define spc ' '
#define yes "YES"
#define no "NO"
#define endl "\n"
const int INF = 1e18;
const int maxn = 2e5 + 5;
const int mod = 1000000000;
const int logn = 20;

void solve() {
	int n, p, q; cin >> n >> p >> q;
	vi a(n);
	for(auto &i : a) cin >> i;
	sort(a.begin(), a.end());
	a.erase(unique(a.begin(), a.end()), a.end());
	n = a.size();

	if(p + q >= n) {
		cout << 1;
		return;
	}

	int limP = min(p, n);
	int limQ = min(q, n);

	auto check = [&](int w) {
		int dp[2005][2005];
		vector<int> nextp(n + 1), nextq(n + 1);
		int r1 = 0, r2 = 0;
		
		for(int i = 0; i < n; i++) {
			while(r1 < n && a[r1] < a[i] + w) r1++;
			nextp[i] = r1;
			while(r2 < n && a[r2] < a[i] + 2LL * w) r2++;
			nextq[i] = r2;
		}
		nextp[n] = nextq[n] = n;

		for(int i = 0; i <= limP; i++) {
			for(int j = 0; j <= limQ; j++) {
				dp[i][j] = 0;
			}
		}

		for(int i = 0; i <= limP; i++) {
			for(int j = 0; j <= limQ; j++) {
				if(i < limP) {
					dp[i + 1][j] = max(dp[i + 1][j], nextp[dp[i][j]]);
				}
				if(j < limQ) {
					dp[i][j + 1] = max(dp[i][j + 1], nextq[dp[i][j]]);
				}
				if(dp[i][j] >= n) return true;
			}
		}
		return false;
	};

	int l = 1, r = 1e9, ans = 1e9;
	while(l <= r) {
		int m = l + (r - l) / 2;
		if(check(m)) {
			ans = m;
			r = m - 1;
		} else {
			l = m + 1;
		}
	}
	cout << ans;
}

signed main() {
	// freopen("disrupt.in", "r", stdin);
	// freopen("disrupt.out", "w", stdout);
	nitro
	int t = 1;
	// cin >> t;
	for(int i = 1; i <= t; i++) {
		// cout << "Case #" << i << ": ";
		solve();
		cout << endl;
	}
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...