Title Link: http://poj.org/problem?id=2385
Idea: The problem of moving rules. DP[I][J] Indicates the maximum number of times that a J can be moved to the Apple by the first minute.
Transfer equation: 1. The current position coincides with the position of the apple to fall dp[i][j] = max (dp[i-1][j]+1,dp[i-1][j-1])
2. Dp[i][j] = max (dp[i-1][j],dp[i-1][j-1]+1) when the current position is inconsistent with where the apple is going to fall
Also note that when J is 0 o'clock, it is not possible to move, separating out
Code:
#include <iostream>#include<cstring>using namespacestd;intdp[1001][ to];inta[1001];intMain () {intn,s; intRes; while(cin>>n>>s) {memset (DP,0,sizeof(DP)); for(intI=1; i<=n;i++) {cin>>A[i]; for(intj =0; j<=s;j++) { if((s-j)%2+1==A[i]) { if(j==0) Dp[i][j]= dp[i-1][j]+1; ElseDp[i][j]= Max (dp[i-1][j]+1, dp[i-1][j-1]); } Else { if(j==0) Dp[i][j]= dp[i-1][j]; ElseDp[i][j]= Max (dp[i-1][j],dp[i-1][j-1]+1); } }} Res=0; for(intI=0; i<=s;i++) {res= Res>dp[n][i]?Res:dp[n][i]; } cout<<res<<Endl; } return 0;}