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>
#define int long long
#define fi first
#define se second
using namespace std;
const int inf = 1e9;
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, p, q;
cin >> n >> p >> q;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) cin >> a[i];
sort(a.begin() + 1, a.end());
int lf = 0, rg = inf, ans = -1;
while (lf <= rg) {
int md = (lf + rg) / 2;
vector<vector<pair<int, int>>> dp(n + 1, vector<pair<int, int>>(2, {inf, inf}));
dp[0][0] = {0, 0};
dp[0][1] = {0, 0};
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
if (a[i] - a[j] + 1 <= md) {
dp[i][0] = min(dp[i][0], {dp[j - 1][0].fi + 1, dp[j - 1][0].se});
dp[i][0] = min(dp[i][0], {dp[j - 1][1].se + 1, dp[j - 1][1].fi});
}
if (a[i] - a[j] + 1 <= 2 * md) {
dp[i][1] = min(dp[i][1], {dp[j - 1][0].se + 1, dp[j - 1][0].fi});
dp[i][1] = min(dp[i][1], {dp[j - 1][1].fi + 1, dp[j - 1][1].se});
}
}
}
int ok = 0;
if (dp[n][0].fi <= p && dp[n][0].se <= q) ok = 1;
if (dp[n][1].fi <= q && dp[n][1].se <= p) ok = 1;
if (ok) {
ans = md;
rg = md - 1;
} else {
lf = md + 1;
}
}
cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |