#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define F first
#define S second
using namespace std;
const int N = 2000 + 10;
const int mod = 1e9 + 7;
const int inf = 1e18;
int n,p,q,a[N];
bool check(int x) {
vector<vector<int>>dp(n + 1,vector<int>(p + 2,inf));
for(int i = 0; i <= p; i++) dp[0][i] = 0;
for(int i = 1; i <= n; i++) {
for(int j = 0; j <= p; j++) {
if(j > 0) dp[i][j] = min(dp[i][j],dp[i - 1][j - 1]);
dp[i][j] = min(dp[i][j],dp[i - 1][j] + 1);
int l = i,r = n,ans = -1;
while(l <= r) {
int mid = (l + r) / 2;
if(a[i] + x - 1 >= a[mid]) {
ans = mid;
l = mid + 1;
}else {
r = mid - 1;
}
}
if(ans != -1) dp[ans][j + 1] = min(dp[ans][j + 1],dp[i - 1][j]);
l = i,r = n,ans = -1;
while(l <= r) {
int mid = (l + r) / 2;
if(a[i] + 2 * x - 1 >= a[mid]) {
ans = mid;
l = mid + 1;
}else {
r = mid - 1;
}
}
if(ans != -1) dp[ans][j] = min(dp[ans][j],dp[i - 1][j] + 1);
}
}
if(dp[n][p] <= q) return true;
return false;
/*for(int i = 1; i <= n; i++) {
for(int j = 0; j <= p; j++) cout << dp[i][j] << " ";
cout << endl;
}*/
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> p >> q;
for(int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1,a + n + 1);
int l = 1,r = 1e9,ans = -1;
while(l <= r) {
int mid = (l + r) / 2;
if(check(mid)) {
ans = mid;
r = mid - 1;
}else {
l = mid + 1;
}
}
cout << ans;
//cout << check(9);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Execution timed out |
1045 ms |
80468 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Execution timed out |
1086 ms |
24384 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |