This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define all(x) x.begin(), x.end()
const int N = 2e6+10, M = 5e3 + 10, MOD = 1e9+7;
int n, d, a[N], T, dp[M][M][2], pref[N][2], suf[N], ans, pre[M][M];
void solve(){
cin >> n >> d >> T;
for(int i = 1; i <= n; ++i) cin >> a[i];
pref[0][0] = 0, pref[0][1] = MOD, suf[n + 1] = 0;
int cur = MOD;
for(int i = 1; i <= n; ++i){
pref[i][0] = pref[i - 1][0];
if(min(a[i], cur) <= T) pref[i][0]++;
cur = min(a[i] + 1, cur + 1);
pref[i][1] = cur - 1;
}
ans = n;
if(n > 5000){
deque<pair<int, int>> q;
for(int i = n; i >= 1; --i){
suf[i] = suf[i + 1];
q.push_front({a[i], i});
while(!q.empty()){
if(min(q.front().first, a[i] + q.front().second - i) <= T){
suf[i]++;
q.pop_front();
}else{
break;
}
}
}
for(int i = 0; i <= n; ++i){
ans = min(ans, pref[i][0] + suf[i + 1]);
}
}else{
for(int i = 0; i <= n; ++i) for(int j = 0; j <= n; ++j) pre[i][j] = 0;
for(int i = 1; i <= n; ++i){
pre[i][i] = a[i] <= T;
int cur = a[i] + 1;
for(int j = i + 1; j <= n; ++j){
pre[i][j] = pre[i][j - 1];
if(min(a[j], cur) <= T) pre[i][j]++;
cur = min(a[j] + 1, cur + 1);
}
}
for(int i = 0; i <= n; ++i) for(int j = 0; j <= n; ++j) dp[i][j][0] = (j == 0 ? pref[i][0] : n), dp[i][j][1] = pref[i][1];
dp[0][0][0] = 0;
dp[0][0][1] = MOD;
for(int i = 1; i <= n; ++i){
bool is = a[i] <= T;
for(int j = 1; j <= d; ++j){
bool added = min(a[i], dp[i - 1][j][1] + 1) <= T;
if(dp[i - 1][j - 1][0] + is > dp[i - 1][j][0] + added){
dp[i][j][0] = dp[i - 1][j][0] + added;
dp[i][j][1] = min(a[i], dp[i - 1][j][1] + 1);
}else{
dp[i][j][0] = dp[i - 1][j - 1][0] + is;
dp[i][j][1] = a[i];
}
}
}
ans = dp[n][d][0];
}
cout << ans;
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
solve();
return 0;
}
# | 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... |