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>
const int MAXN = 4010 ;
const int MAX = 2e6+10 ;
using namespace std ;
int N , D , T , toGet, toFill = 1 ;
int t[MAX] ;
int dp[2][MAXN];
int cost[MAXN][MAXN] ;
void solve(int l, int r, int optmin , int optmax )
{
if(l > r) return ;
int mid = (l+r)>>1 ;
int opt = optmin ;
dp[toFill][mid] = cost[opt][mid]+dp[toGet][opt-1] ;
for(int i = optmin+1 ; i <= min(mid,optmax) ; i++ )
{
int newCost = cost[i][mid] + dp[toGet][i-1] ;
if(newCost >= dp[toFill][mid]) continue ;
opt = i ;
dp[toFill][mid] = newCost ;
}
solve(l, mid-1, optmin, opt ) ;
solve(mid+1, r, opt, optmax );
}
void solve2()
{
vector<int> dpSuf(N+2,0) ;
vector<int> r(N+1,N+1) ;
for(int i = N ; i > 0 ; i-- )
{
r[i] = i+1 ;
while( r[i] <= N && t[i]+r[i]-i < t[r[i]] ) r[i] = r[ r[i] ] ;
int k = T-t[i]+i ;
dpSuf[i] = dpSuf[r[i]]+max(0,min( r[i]-1 , k )-i+1 ) ;
}
int s = t[1]+1 ;
int qtd = t[1] <= T ;
int ans = qtd+dpSuf[2] ;
for(int i = 2 ; i <= N ; i++ , s++ )
{
s = min(s, t[i] ) ;
qtd += s <= T ;
ans = min(ans, qtd+dpSuf[i+1]) ;
}
printf("%d\n" , ans ) ;
exit(0) ;
}
int main()
{
scanf("%d %d %d", &N, &D, &T ) ;
for(int i = 1 ; i <= N ; i++ ) scanf("%d", &t[i] ) ;
if( D <= 1 ) solve2() ;
for(int i = 1 ; i <= N ; i++ )
{
int s = t[i]+1 ;
cost[i][i] = t[i] <= T ;
for(int j = i+1 ; j <= N ; j++ , s++ )
{
s = min(s, t[j] ) ;
cost[i][j] = cost[i][j-1]+(s <= T ) ;
}
}
for(int i = 1 ; i <= N ; i++ ) dp[0][i] = cost[1][i] ;
for(int i = 1 ; i <= D ; i++ , swap(toGet, toFill) )
{
for(int j = 1 ; j <= i ; j++ ) dp[toFill][j] = dp[toGet][j] ;
solve(i+1, N, i+1, N ) ;
}
printf("%d\n", dp[toGet][N] ) ;
}
Compilation message (stderr)
prison.cpp: In function 'int main()':
prison.cpp:70:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
70 | scanf("%d %d %d", &N, &D, &T ) ;
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
prison.cpp:71:38: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
71 | for(int i = 1 ; i <= N ; i++ ) scanf("%d", &t[i] ) ;
| ~~~~~^~~~~~~~~~~~~~
# | 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... |