# poj2385_dp

dp[1][1]=1;

dp[i][j]=dp[i-1][j]+array[i]%2   (j==0)

dp[i][j]=max(dp[i][j],dp[i-1][j-1]+(array[i]==j%2+1)); (j<=i)
dp[i][j]=max(dp[i][j],dp[i-1][j]+(array[i]==j%2+1));    (j<=i)

View Code
 1 #include <iostream>
2 #include <stdio.h>
3 #include <string.h>
4 using namespace std;
5 //276K 0MS
6 //dp
7 int dp[1001][31];
8 int array[1001];
9
10 int main()
11 {
12     int t,w,i,j;
13     memset(dp,0,sizeof(dp));
14     scanf("%d%d",&t,&w);
15     for(i=1;i<=t;i++)
16         scanf("%d",&array[i]);
17     if(array[1]==1)
18         dp[1][0]=1;
19     dp[1][1]=1;
20     for(i=2;i<=t;i++)
21         for(j=0;j<=w;j++)
22         {
23             if(j==0)
24             {
25                 dp[i][j]=dp[i-1][j]+array[i]%2;
26                 continue;
27             }
28             if(j<=i)
29             {
30                 dp[i][j]=max(dp[i][j],dp[i-1][j-1]+(array[i]==j%2+1));
31                 dp[i][j]=max(dp[i][j],dp[i-1][j]+(array[i]==j%2+1));
32             }
33         }
34     printf("%d\n",dp[t][w]);
35     return 0;
36 }

posted @ 2012-08-26 11:02  pushing my way  阅读(253)  评论(0编辑  收藏  举报