Submission #409974

#TimeUsernameProblemLanguageResultExecution timeMemory
409974CaroLindaThe short shank; Redemption (BOI21_prison)C++14
35 / 100
128 ms45744 KiB
#include <bits/stdc++.h>

const int MAXN = 4010 ;

using namespace std ;

int N , D , T , toGet, toFill = 1 ;
int t[MAXN] ;
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:69:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |  scanf("%d %d %d", &N, &D, &T  ) ;
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
prison.cpp:70:38: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |  for(int i = 1 ; i <= N ; i++ ) scanf("%d", &t[i] ) ;
      |                                 ~~~~~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...