이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
const int N = 2000;
const int A = 1e9;
const int INF = 1e9;
int n, p, q;
int a[N + 5];
int nextP[N + 5], nextQ[N + 5];
int memo[N + 5][N + 5];
int dp(int idx, int p) {
if (p < 0) return INF;
if (idx >= n) return 0;
int &ret = memo[idx][p];
if (memo[idx][p] != -1) return ret;
ret = min(dp(nextP[idx], p - 1), dp(nextQ[idx], p) + 1);
return ret;
}
bool solve(int w) {
int j = 1, k = 1;
for(int i = 0; i < n; i++){
while(j < n && a[j] - a[i] + 1 <= w) j++;
while(k < n && a[k] - a[i] + 1 <= 2*w) k++;
nextP[i] = j;
nextQ[i] = k;
}
memset(memo, -1, sizeof(memo));
return dp(0, p) <= q;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> p >> q;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
if (p + q >= n) {
cout << 1 << "\n";
return 0;
}
sort(a, a + n);
int l = 1, r = A, ans = INF;
while (l <= r) {
int mid = (l + r) / 2;
if (solve(mid)) {
ans = min(ans, mid);
r = mid - 1;
} else {
l = mid + 1;
}
}
cout << ans << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |