学了三天 DP 连个 P 都不会,总的来说就是寄了,每道题不看题解就做不来,试着做了做三道提高组难度的题,做出来了俩,另外一个没有思路。这俩题我都想出了大部分思路,但最后几步由于经验问题没想出来。做完后我以为我懂了,然后膨胀了,去挑战低价购买这道题。然后不出意外寄了。打算先不弄这个了,等以后在弄,先把之前写的贴上来吧。
int tots , totc , dp[2][2000006] ; int T = 1000000 ; char a[2000006] ;
int main ( ) { int n ; scanf ( "%d" , & n ) ; int l = 0 , r = 0 ; for ( int i = 0 ; i <= 2000006 ; i ++ ) dp[0][i] = dp[1][i] = -100000006 ; dp[0][T] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { scanf ( "%s" , a ) ; int len = strlen ( a ) , tots = 0 , totc = 0 ; for ( int j = 0 ; j < len ; j += 2 ) a[j] == 's' ? tots ++ : totc ++ ; int v = totc , w = tots - totc ; l = min ( l + w , l ) ; r = max ( r + w , r ) ; for ( int j = l ; j <= r ; j ++ ) { dp[i&1][T+j] = max ( dp[i&1^1][T+j] , dp[i&1][T+j] ) ; dp[i&1][T+j] = max ( dp[i&1^1][T+j-w] + v , dp[i&1][T+j] ) ; } } cout << dp[n&1][T] ; return 0 ; }
struct L { int t , f , h ; } trash[1005] ; int dp[105][105] ; bool cmp ( L x , L y ) { return x.t < y.t ; }
int main ( ) { int D , G ; scanf ( "%d%d" , & D , & G ) ; for ( int i = 1 ; i <= G ; i ++ ) scanf ( "%d%d%d" , & trash[i].t , & trash[i].f , & trash[i].h ) ; sort ( trash + 1 , trash + 1 + G , cmp ) ; dp[0][0] = 10 ; for ( int i = 1 ; i <= G ; i ++ ) { for ( int j = 0 ; j <= D ; j ++ ) { if ( j >= trash[i].h && dp[i-1][j-trash[i].h] >= trash[i].t ) { dp[i][j] = max ( dp[i-1][j-trash[i].h] ,dp[i][j] ) ; } if ( dp[i-1][j] >= trash[i].t ) { dp[i][j] = max ( dp[i-1][j] + trash[i].f , dp[i][j] ) ; } } } for ( int i = 1 ; i <= G ; i ++ ) { if ( dp[i][D] != 0 ) { cout << trash[i].t ; return 0 ; } } int now = 10 ; for ( int i = 1 ; i <= G; i ++ ) { if ( now < trash[i].t - trash[i-1].t ) { cout << trash[i-1].t + now ; return 0 ; } else now = now - trash[i].t + trash[i-1].t + trash[i].f ; } cout << trash[G].t + now ; return 0 ; }