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;
#define endl '\n'
int dp[4005][2];
int cost[4005][4005];
void calc(int l,int r,int opt1,int opt2,int k){
if(l > r) return;
int i = l + r >> 1;
pair<int,int> opt{1e9,-1};
for(int p=opt1;p<=min(opt2,r);p++){
if(p) opt = min(opt,{dp[p-1][k-1&1]+cost[p][i],p});
else opt = min(opt,{cost[0][i],p});
}
dp[i][k&1] = opt.first;
calc(l,i-1,opt1,opt.second,k);
calc(i+1,r,opt.second,opt2,k);
}
void solve(){
int n,d,t;
cin>>n>>d>>t;
vector<int> v(n);
for(auto &i:v) cin>>i;
for(int i=0;i<n;i++){
int lst = v[i];
cost[i][i] = (lst <= t);
for(int j=i+1;j<n;j++) {
cost[i][j] += cost[i][j-1];
if(v[j] > lst) lst ++;
else lst = v[j];
cost[i][j] += (lst <= t);
}
}
for(int i=0;i<n;i++) dp[i][0] = cost[0][i];
for(int k=1;k<=d;k++){
calc(0,n-1,0,n-1,k);
}
cout<<dp[n-1][d&1]<<endl;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t=1;
//cin>>t;
while(t--){
solve();
}
return 0;
}
Compilation message (stderr)
prison.cpp: In function 'void calc(int, int, int, int, int)':
prison.cpp:9:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
9 | int i = l + r >> 1;
| ~~^~~
prison.cpp:12:39: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
12 | if(p) opt = min(opt,{dp[p-1][k-1&1]+cost[p][i],p});
| ~^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |