BZOJ1750: [Usaco2005 qua]Apple Catching

n<=1000分钟,每分钟会有一个苹果从树1或树2掉下,一开始在树1,最多换位置m<=30次,问最多能收到多少苹果。

F(i,j,0/1)--前i分钟在换位置j次,在树1/2:

 1 #include<cstring>
 2 #include<cstdlib>
 3 #include<cstdio>
 4 //#include<assert.h>
 5 //#include<time.h>
 6 #include<math.h>
 7 #include<algorithm>
 8 //#include<iostream>
 9 using namespace std;
10 
11 bool isdigit(char c) {return c>='0' && c<='9';}
12 int qread()
13 {
14     char c;int s=0,f=1;while (!isdigit(c=getchar())) f=(c=='-'?-1:1);
15     do s=s*10+c-'0'; while (isdigit(c=getchar())); return s*f;
16 }
17 
18 int n,m,a[1011],f[1011][33][2];
19 int main()
20 {
21     n=qread(),m=qread();
22     for (int i=1;i<=n;i++) a[i]=qread()-1;
23     for (int i=0;i<=m;i++) f[0][i][0]=0,f[0][i][1]=-0x3f3f3f3f;
24     int ans=0;
25     for (int i=1;i<=n;i++)
26     {
27         f[i][0][a[i]]=f[i-1][0][a[i]]+1;
28         f[i][0][a[i]^1]=f[i-1][0][a[i]^1];
29         ans=max(ans,max(f[i][0][0],f[i][0][1]));
30         for (int j=1;j<=m;j++)
31         {
32             f[i][j][a[i]]=max(f[i-1][j][a[i]],f[i-1][j-1][a[i]^1])+1;
33             f[i][j][a[i]^1]=max(f[i-1][j-1][a[i]],f[i-1][j][a[i]^1]);
34             ans=max(ans,max(f[i][j][0],f[i][j][1]));
35         }
36     }
37     printf("%d\n",ans);
38     return 0;
39 }
View Code

 

posted @ 2017-10-26 14:25  Blue233333  阅读(167)  评论(0编辑  收藏  举报