poj-2385Apple Catching(dp)

题目链接:http://poj.org/problem?id=2385

Description

It is a little known fact that cows love apples. Farmer John has two apple trees (which are conveniently numbered 1 and 2) in his field, each full of apples. Bessie cannot reach the apples when they are on the tree, so she must wait for them to fall. However, she must catch them in the air since the apples bruise when they hit the ground (and no one wants to eat bruised apples). Bessie is a quick eater, so an apple she does catch is eaten in just a few seconds. 

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

* 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.

题目大意:两颗苹果树,每秒会在其中一颗树上掉下一个苹果(这么神奇得??),一头牛(又是牛??)一开始在第一颗树下,牛想吃苹果,但是又很懒,想用不多于w的步数吃到最多得没掉到地上的苹果(苹果掉下的时候在树下),问最多能'吃到多少个苹果

两棵树,模拟一下;

用样例看一下:2 1 1 2 2 1 1,一开始是在1处,从头到尾走0步(懒到1步都不想走),从头到尾走1步,从头到尾走2步,只能走两步了。

列个表就很清楚了:

步数\秒数 1 2 3 4 5 6 7
0 0 1 2 2 2 3 4
1 1 1 2 3 4 4 4
2 1 2 3 3 4 5 6

对于这个表,0步很容易理解,一直呆在第一颗树下,有苹果就吃,没就不吃,因此就是表中的数据;

1步,牛可与选择在任何时刻走这一步,因此,第一秒,可以走出去,吃一个苹果

第二秒,因为只能走一步,所以要么第一步走出去,不吃,要么第一步没走,吃,但是无所谓,都是就吃1个,所以走哪一个都行,

第三秒,不走的话可以吃到两个,走的话只能吃1个了,所以牛第一步就不走了,同理后面的一样;

.....

两步的就是从第一步推过来的,第一秒就走两步,.....

然后判断走几步在那个苹果树xi下就行了,因为只有两颗,所以走偶数次的话总能hui'回到原位置,奇数次总是离开原位置,所以可以n(步)%2判断在那个树下,因为一开始实在第一颗树下,就很好判断了。

状态转移方程:dp[i][j]=max(dp[i-1][j-1],dp[i-1][j])    走哪里能吃到更多的苹果,再判断是否在树下,

ac:

#include<stdio.h>
#include<string.h>  
#include<math.h>  
  
//#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<iostream>  
#include<algorithm>  
using namespace std;  
  
#define ll long long  
#define da    0x3f3f3f3f  
#define clean(a,b) memset(a,b,sizeof(a))// 水印 

int dp[1100][35];
int shuzu[1100];

int main()
{
	int t,w;
	cin>>t>>w;
	for(int i=0;i<t;++i)
	{
		cin>>shuzu[i];
		shuzu[i]=shuzu[i]-1;//偶数次在第一颗树下,奇数次在第二颗树下 
	}
	for(int i=0;i<=w;++i)
	{
		if(i%2==shuzu[0])
			dp[0][i]=1;
	}
	for(int i=1;i<t;++i)
	{
		for(int j=0;j<=w;++j)//这里用j次的步数 
		{
			if(j==0)
				dp[i][j]=dp[i-1][j];//一直呆在树下只有一种情况 
			else
				dp[i][j]=max(dp[i-1][j-1],dp[i-1][j]);//这一步到树下|之前到树下 
			if(j%2==shuzu[i])
				dp[i][j]++;
		}
	}
	int res=-1;
	for(int i=0;i<=w;++i)
		res=res>dp[t-1][i]?res:(dp[t-1][i]);
	cout<<res<<endl;
}

猜你喜欢

转载自blog.csdn.net/qq_40482358/article/details/81075127