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>
#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 (stderr)
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;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |