#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
long long get_rand() {
long long a = rand();
long long b = rand();
return a * (RAND_MAX + 1ll) + b;
}
#define int long long
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
#define intmin -100000000000000000LL
#define intmax 100000000000000000LL
vector<int> arr;
int n, p, q;
bool check(int m){
//I will require total at most n cameras
vector<vector<int>> dp(n+2, vector<int>(n+2, 0)); //position, small cameras, will be at most n
for (int i = 1; i <= n; ++i)
{
int sOne = lower_bound(arr.begin(), arr.end(), arr[i]-(2*m)+1) - arr.begin();
dp[i][0] = dp[max(0LL, sOne-1)][0] + 1;
for (int j = 1; j <= min(p, i); ++j) //use those many small cameras max
{
//first position behind ith with diff <= m, aka use a small camera;
int one = lower_bound(arr.begin(), arr.end(), arr[i]-m+1) - arr.begin() - 1;
one = max(0LL, one);
//first position behind ith with diff <= 2*m, aka use a big camera;
int two = lower_bound(arr.begin(), arr.end(), arr[i]-(m*2)+1) - arr.begin() - 1;
two = max(0LL, two);
dp[i][j] = min(dp[one][j-1], dp[two][j]+1);
}
}
for (int i = 0; i <= min(p, n); ++i)
{
if(dp[n][i] <= q){
return true;
}
}
return false;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> p >> q;
arr.assign(n+1, 0);
arr[0] = 0;
for (int i = 1; i <= n; ++i)
{
cin >> arr[i];
}
sort(arr.begin(), arr.end());
int l = 0, r = 10000000000, mid = (l+r)/2, ans;
while(l <= r){
mid = (l+r)/2;
if(check(mid)){
ans = mid;
r = mid - 1;
}
else{
l = mid + 1;
}
}
cout << max(ans, 1LL) << '\n';
}
Compilation message
watching.cpp: In function 'int32_t main()':
watching.cpp:70:48: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
70 | int l = 0, r = 10000000000, mid = (l+r)/2, ans;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
3 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
3 ms |
392 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
5 ms |
396 KB |
Output is correct |
12 |
Correct |
3 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
458 ms |
32080 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Execution timed out |
1087 ms |
31860 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |