简单dp- Apple Catching

奶牛爱吃苹果是鲜为人知的事实。FJ在他的田地里有两棵苹果树(编号为1和2)。Bessie不会爬树,所以她必须等着苹果掉下来。然而,她必须在空中接住它们,因为苹果落地时碰伤了(谁也不想吃碰伤了的苹果)。Bessie吃得很快,吃的时间可以忽略不计。 每分钟,两棵苹果树中的一棵会掉落一个苹果。Bessie有过很多练习,只要她站在一棵有苹果落下的树下,就能接住一个苹果。Bessie可以很快地在两棵树之间行走,时间忽略不计。她可以随时站在一棵树下。此外,奶牛没有得到大量的锻炼,所以她不愿意在树间来回地来回行走(因此错过了一些苹果)。 苹果下降T(1 < = T< = 1000)分钟。Bessie愿意来回走至多W(1 < = W< = 30)次。考虑哪棵树每分钟会掉落一个苹果,确定贝西能抓到的最大数量的苹果。贝西从树1开始。

Input

* Line 1: Two space separated integers: T and W * Lines 2..T+1: 1 or 2: the tree that will drop an apple each minute.

Output

* Line 1: The maximum number of apples Bessie can catch without walking more than W times.

Sample Input

7 2
2
1
1
2
2
1
1

Sample Output

6

Hint

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. 

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.

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
int dp[1009][1009];//i为第几秒j为几次走 
int v[100009]; 
int main()
{
	int t,w;
	scanf("%d%d",&t,&w);
	for(int i = 1;i <= t;++i)
	{
		scanf("%d",&v[i]);
	}
	int ans = 0;
	for(int i = 1;i <= t;++i)
	{
		for(int j = 0;j <= w;++j)
		{
			if(j == 0)
			{
				dp[i][j] = dp[i-1][j];
			}//初始化配合下面的j为奇数且为树2++,和j为偶数且树1++ 
			else{
			dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]);//有两个方向一个为不换方向-个换方向	
			}
			if((j&1 && v[i] == 2) || (!(j&1) && v[i] == 1))
			{
				dp[i][j]++;
			} //判断是否会掉下苹果 j为奇数且为树2++,和j为偶数且树1++ 
			ans = max(ans,dp[i][j]);//可能是不用那么多步所以找每步的最大值 
		}
	}
	printf("%d\n",ans);
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章