Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8855 | Accepted: 4311 |
Description
Each minute, one of the two apple trees drops an apple. Bessie, having much practice, can catch an apple if she is standing under a tree from which one falls. While Bessie can walk between the two trees quickly (in much less than a minute), she can stand under only one tree at any time. Moreover, cows do not get a lot of exercise, so she is not willing to walk back and forth between the trees endlessly (and thus misses some apples).
Apples fall (one each minute) for T (1 <= T <= 1,000) minutes. Bessie is willing to walk back and forth at most W (1 <= W <= 30) times. Given which tree will drop an apple each minute, determine the maximum number of apples which Bessie can catch. Bessie starts at tree 1.
Input
* Lines 2..T+1: 1 or 2: the tree that will drop an apple each minute.
Output
Sample Input
7 2 2 1 1 2 2 1 1
Sample Output
6
Hint
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.
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.
题意:有两棵苹果树,标号分别为1,2。每分钟有其中的一棵树会掉下一个苹果,奶牛一分钟只能在其中一棵树下接到苹果,但她不知道下一分钟会是那棵树掉下苹果,所以她就要在两棵树下来回跑。但她只会在树下跑W次。问你在T分钟内,奶牛bessie最多能接到多少苹果。
仔细看能看出,是一道dp题,需要递推整个过程才能完成最终的求解。
定义dp[i][j]为奶牛在第i分钟,移动j次以后得到的最大苹果数量
给出状态转移方程:dp[i][j]=max(dp[i-1][j],dp[i-1][j-1])+count。这里的dp[i][j]代表在第i分钟移动j次最多能接到的苹果数。在第i分钟奶牛到某棵树下有两种状态:1.从另一棵树走过来(dp[i-1][j-1])2.本来就呆在这棵树下(dp[i-1][j])
在这里我设置了三维数组dp[i][j][k],第三维度来记录下当前奶牛站的位置(k=0或1),在做状态转移的时候,只要两种状态都表示出来就可以。在表示时,需要把dp[i][j][0]和dp[i][j][1]存储的最大苹果数量都要记录下来,最后取最大值即可。
最后初始化,根据上面的状态转移方程以及题意,不难想出吧,具体见下面的代码。
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int dp[1005][35][2];
//第i分钟移动j次得到最大的苹果数量
//第三维:0 表示站在第一树下
// 1 表示站在第二树下
int num[1005];
int main()
{
int T,W;
cin>>T>>W;
int maxsum = 0;
for(int i = 1; i <= T; i++)
scanf("%d",&num[i]);
for(int i = 1; i <= T; i++)
{
dp[i][0][0] = dp[i-1][0][0] + (num[i] ==1);
dp[i][0][1] = dp[i-1][0][1] + (num[i] ==2);
for(int j = 1; j <= W; j++)
{
dp[i][j][0] = max(dp[i-1][j-1][1],dp[i-1][j][0])+(num[i]==1);
dp[i][j][1] = max(dp[i-1][j-1][0],dp[i-1][j][1])+(num[i]==2);
maxsum = max(maxsum,max(dp[i][j][0],dp[i][j][1]));
}
}
cout<<maxsum<<endl;
return 0;
}