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;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const ll oo = 1e18;
const int mod = 1e9+7;
const int nax = 2020;
int n,s,b;
int dp[nax][nax];
vector<int> a;
bool can(int w){
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
dp[i][j] = 2e9;
int to = -1,u = 0;
for(int i=0;i<n;i++){
if(a[i] > to){
to = a[i] + w - 1;
u++;
}
dp[i][0] = u;
}
for(int i=0;i<n;i++){
for(int j=1;j<=b;j++){
int cur = a[i] - 2*w + 1;
if(cur <= 0)dp[i][j] = 0;
else{
int at = lower_bound(a.begin(),a.end(),cur) - a.begin();
if(at == 0){
dp[i][j] = 0;
}else{
at--;
dp[i][j] = min(dp[i][j],dp[at][j-1]);
// if(i)dp[i][j] = min(dp[i][j],dp[i-1][j]+1);
}
}
cur = a[i] - w + 1;
if(cur <= 0)dp[i][j] = 0;
else{
int at = lower_bound(a.begin(),a.end(),cur) - a.begin();
if(at == 0){
dp[i][j] = 0;
}else{
at--;
dp[i][j] = min(dp[i][j],dp[at][j]+1);
// if(i)dp[i][j] = min(dp[i][j],dp[i-1][j]+1);
}
}
}
}
// if(w == 9)cout << dp[7][2] << endl;
return dp[n-1][b] <= s;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
#endif
cin>>n>>s>>b;
s = min(s,n);
b = min(b,n);
a.resize(n);
for(int i=0;i<n;i++)cin>>a[i];
sort(a.begin(),a.end());
int md,lo=0,hi=1e9,ans = 0;
while(lo <= hi){
md = (lo + hi)/2;
if(can(md)){
ans = md;
hi = md-1;
}else lo = md+1;
}
cout << ans << endl;
}
Compilation message (stderr)
watching.cpp: In function 'int main()':
watching.cpp:62:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen("input.txt","r",stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |