#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1010;
int a[N + 20];
int dp[N + 20][N + 20];
int n;
bool Cost(int w, int p, int q) {
int forP[n + 1], forQ[n + 1];
for(int i = 0; i <= n; i++) {
forP[i] = 0;
forQ[i] = 0;
}
int prev = 1;
for(int i = 1; i <= n; i++) {
for(int j = prev; j <= n; j++) {
if(a[j] - a[i] >= w) {
prev = j;
break;
}
else if(j == n) {
prev = n + 1;
}
}
forP[i] = prev - 1;
}
prev = 1;
for(int i = 1; i <= n; i++) {
for(int j = prev; j <= n; j++) {
if(a[j] - a[i] >= 2 * w) {
prev = j;
break;
}
else if(j == n) {
prev = n + 1;
}
}
forQ[i] = prev - 1;
}
/*cout << "forP array\n";
for(int i = 1; i <= n; i++) {
cout << forP[i] << " ";
}
cout << "\n";
cout << "forQ array\n";
for(int i = 1; i <= n; i++) {
cout << forQ[i] << " ";
}
cout << "\n";*/
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
dp[i][j] = 0;
}
}
for(int i = 1; i <= p; i++) {
dp[i][0] = (dp[i - 1][0] == n) ? dp[i - 1][0] : forP[dp[i - 1][0] + 1];
}
for(int j = 1; j <= q; j++) {
dp[0][j] = (dp[0][j - 1] == n) ? dp[0][j - 1] : forQ[dp[0][j - 1] + 1];
}
for(int i = 1; i <= p; i++) {
for(int j = 1; j <= q; j++) {
if(dp[i - 1][j] == n || dp[i][j - 1] == n) {
dp[i][j] = n;
continue;
}
dp[i][j] = max((forP[dp[i - 1][j] + 1]), (forQ[dp[i][j - 1] + 1]));
}
}
if(dp[p][q] == n)
return true;
return false;
}
void Solve() {
int p, q;
cin >> n >> p >> q;
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
sort(a + 1, a + 1 + n);
if(p + q >= n) {
cout << "1\n";
return;
}
int left = 1, right = 1e9;
int mid, ans = 1e9 + 1;
while(left <= right) {
//cout << "left = " << left << ", right = " << right << "\n";
mid = (left + right) / 2;
//cout << "mid = " << mid << "\n";
if(Cost(mid, p, q)) {
//cout << "mid is coming true\n";
right = mid - 1;
ans = mid;
}
else {
//cout << "mid is coming false\n";
left = mid + 1;
}
}
cout << ans << "\n";
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
Solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
8428 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
32 ms |
8428 KB |
Output is correct |
4 |
Correct |
2 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
33 ms |
8428 KB |
Output is correct |
8 |
Correct |
29 ms |
8448 KB |
Output is correct |
9 |
Correct |
28 ms |
8448 KB |
Output is correct |
10 |
Correct |
32 ms |
8684 KB |
Output is correct |
11 |
Correct |
29 ms |
8428 KB |
Output is correct |
12 |
Correct |
30 ms |
8428 KB |
Output is correct |
13 |
Correct |
31 ms |
8428 KB |
Output is correct |
14 |
Correct |
29 ms |
8428 KB |
Output is correct |
15 |
Correct |
35 ms |
8428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
620 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |