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'
const int N = 75005;
int P[N][20] , dp[N][2] , costt[N][20];
int t;
vector<int> v;
int cost(int l,int r){
int ret = 0;
int cur = l;
for(int k=19;k>=0;k--){
if(P[cur][k] <= r) {
ret += costt[cur][k];
cur = P[cur][k];
}
}
ret += min(max(t-v[cur]+1,0),r-cur+1);
return ret;
}
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,i);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;
cin>>n>>d>>t;
v.resize(n);
for(auto &i:v) cin>>i;
vector<int> starts;
for(int i=n-1;i>=0;i--){
while(starts.size()){
int a = starts.back();
if(v[a] >= v[i]+(a-i)) starts.pop_back();
else break;
}
P[i][0] = (starts.size() ? starts.back() : n);
costt[i][0] = min(max(t-v[i]+1,0),P[i][0] - i);
starts.push_back(i);
}
for(int k=1;k<20;k++){
for(int i=0;i<n;i++){
if(P[i][k-1] == n) P[i][k] = n;
else P[i][k] = P[P[i][k-1]][k-1];
costt[i][k] = costt[i][k-1] + costt[P[i][k-1]][k-1];
}
}
int lst = v[0];
int prefix = (lst <= t);
dp[0][0] = prefix;
for(int j=1;j<n;j++) {
if(v[j] > lst) lst ++;
else lst = v[j];
prefix += (lst <= t);
dp[j][0] = prefix;
}
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:23:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
23 | int i = l + r >> 1;
| ~~^~~
prison.cpp:27:39: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
27 | 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... |