Apple Catching(POJ-2385)

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


题意

Cows 喜欢苹果 , 农夫有两颗苹果树 , Cows可以在两棵苹果树间移动,苹果树每分钟会掉落一个苹果,但是它不想在两棵树间移动超过 w 次,问Cows 最多能吃掉多少苹果

分析

dp[i][j]:
表示 i 时刻 j 次移动获得的苹果数量
状态转移方程:
dp[i][j] = max(dp[i-1][j] , dp[i-1][j-1])
状态转移方程分析:
当Cows处于某一时刻时,这个时刻的状态应该在上一状态(不进行移动)or(进行移动)中二选一进行转移到当前时刻,要使此时刻状态最优,就须取二者中最优的
我们还需要再判断一下第 j 次移动后是否移动到了当前时刻有苹果落下的那棵树(即判断 j % 2 + 1 是否等于 mp[i]),若是则可以多吃一个苹果
最后更新每个时刻收获的苹果最大值


Code

#include
#include
#include
using namespace std;
const int maxn = 1e3+6;
const int INF  = 0x3f3f3f;
int mp[maxn];
int dp[maxn][maxn];
int main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	int t,w;
	cin>>t>>w;
	memset(mp,0,sizeof(mp));
	memset(dp,0,sizeof(dp));
	int ans  = -INF;
	for(int i = 1 ; i <= t ; i++)
	{
		cin>>mp[i];//t时刻有一颗苹果下落在mp[t]
	}
	for(int i = 1 ; i <= t ; i++)
	{
		for(int j = 0 ; j <= w ; j++)
		{
//	当前时刻等于 = 现在处于与上一时刻同一棵树(没有进行移动) 和 现在处于与上一时刻不同树(进行移动) 的最优最大值
			dp[i][j] = max(dp[i-1][j] , dp[i-1][j-1]);
//	j%2+1 :   
//			看看模拟移动之后的位置是否是在当前时间i有苹果下落的那棵树下
//			是的话就可以收获当前时刻当前树的那颗苹果
			if(j % 2 + 1 == mp[i])
			{
				dp[i][j]++;
			}
//			记录每个时刻收获的苹果的最大数
			ans = max(ans,dp[i][j]);
		}
	}
	cout<<ans<<endl;
	return 0;
}

你可能感兴趣的:(动态规划,动态规划,算法)