Description
Input
Output
Sample Input
7 2
2
1
1
2
2
1
1
INPUT DETAILS:
Seven apples fall - one from tree 2, then two in a row from tree 1, then
two in a row from tree 2, then two in a row from tree 1. Bessie is
willing to walk from one tree to the other twice.
Sample Output
6
OUTPUT DETAILS:
Bessie can catch six apples by staying under tree 1 until the first two
have dropped, then moving to tree 2 for the next two, then returning back
to tree 1 for the final two.
题意是一个人站在树下接苹果,树只有两棵,每一个时刻只有一棵树有苹果掉下来,但是人只能从一棵树移到另一棵树最多m次,求最多能接多少个苹果
dp太水了,f[i][j][0 / 1]表示第i时刻已经移动了j次,当前在第1 / 2棵树下的方案,然后转移自己yy一下吧。或者直接看代码
#include<cstdio> inline int max(int a,int b) {return a>b?a:b;} inline int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,m,mx; int f[1001][1001][2];//Ç° i ¸ö¡¢Òƶ¯ j ²½¡¢µ±Ç°Î»ÖÃÊÇ1/2 int a[1001][2]; int main() { scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) { int x=read(); a[i][x-1]=1; } for (int i=1;i<=n;i++) { f[i][0][0]=f[i-1][0][0]+a[i][0]; f[i][0][1]=f[i-1][0][1]+a[i][1]; for (int j=1;j<=m;j++) { f[i][j][0]=max(f[i-1][j-1][1],f[i-1][j][0])+a[i][0]; f[i][j][1]=max(f[i-1][j-1][0],f[i-1][j][1])+a[i][1]; mx=max(mx,f[i][j][0]); mx=max(mx,f[i][j][1]); } } printf("%d\n",mx); }
然后我再想了下,好像我们把相邻的相同的数字缩成一个数,用缩掉的数字的个数表示,然后求长度为m+1的最大子串和
比如样例:
7 2
2|1 1|2 2|1 1缩成1 2 2 2
然后显然答案是2 2 2即6
但是有反例
7 2
1 2 1 2 1 2 2
答案是5,这样做是4
我想不用多解释了吧
所以还是老老实实dp吧