POJ 2385
1 #include <algorithm> 2 #include <cstdlib> 3 #include <numeric> 4 #include <iostream> 5 using namespace std; 6 7 int max(int i,int j) 8 { 9 if(i > j) 10 return i; 11 return j; 12 } 13 14 int main() 15 { 16 //freopen("acm.acm","r",stdin); 17 int dp[1005][31]; 18 int tree[1005]; 19 int T; 20 int W; 21 int i; 22 int j; 23 memset(dp,0,sizeof(dp)); 24 cin>>T>>W; 25 for(i = 1;i <= T; ++ i) 26 cin>>tree[i]; 27 for(i = 1; i <= T; ++ i) 28 { 29 dp[i][0] = dp[i-1][0]; 30 if(tree[i] == 1) 31 ++ dp[i][0]; 32 } 33 for(i = 1;i <= T;++ i) 34 { 35 for(j = 1;j <= W;++ j) 36 { 37 if(j > i) 38 break; 39 dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]); 40 if(tree[i] == j%2+1) 41 ++ dp[i][j]; 42 } 43 } 44 cout<<dp[T][W]; 45 return 0; 46 }