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>
using namespace std;
const int MN = 2002;
int pos[MN], N, P, Q, i, lo, hi, l, r, k, nxt[MN], nxt2[MN], dp[MN][MN];
int solve(int n,int rem){
if(n == N+1) return 0;
else if(dp[n][rem]!=-1) return dp[n][rem];
if(rem) dp[n][rem]=min(solve(nxt[n],rem-1),solve(nxt2[n],rem)+1);
else dp[n][rem]=solve(nxt2[n],rem)+1;
return dp[n][rem];
}
bool check(int w){
for(l=r=k=1;l<=N;l++){
while(r<=N&&pos[r]-w+1<=pos[l]) r++;
while(k<=N&&pos[k]-2*w+1<=pos[l]) k++;
nxt[l] = r; nxt2[l] = k;
}
memset(dp, -1, sizeof(dp));
return solve(1,P)<=Q;
}
int main(){
for(scanf("%d%d%d",&N,&P,&Q),i=1;i<=N;i++)
scanf("%d",&pos[i]);
P = min(P, N);
sort(pos+1,pos+N+1);
lo = 1, hi = 1e9+7;
while(lo < hi){
int m = lo+hi>>1;
if(check(m)) hi=m;
else lo=m+1;
}
printf("%d\n",hi);
return 0;
}
Compilation message (stderr)
watching.cpp: In function 'int main()':
watching.cpp:29:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int m = lo+hi>>1;
~~^~~
watching.cpp:23:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(scanf("%d%d%d",&N,&P,&Q),i=1;i<=N;i++)
~~~~~~~~~~~~~~~~~~~~~~~~^~~~
watching.cpp:24:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&pos[i]);
~~~~~^~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |