首页 > ACM题库 > HDU-杭电 > hdu 2713 Jumping Cows-动态规划-[解题报告]C++
2014
02-14

hdu 2713 Jumping Cows-动态规划-[解题报告]C++

Jumping Cows

问题描述 :

Farmer John’s cows would like to jump over the moon, just like the cows in their favorite nursery rhyme. Unfortunately, cows can not jump.

The local witch doctor has mixed up P (1 <= P <= 150,000) potions to aid the cows in their quest to jump. These potions must be administered exactly in the order they were created, though some may be skipped.

Each potion has a ‘strength’ (1 <= strength <= 500) that enhances the cows’ jumping ability. Taking a potion during an odd time step increases the cows’ jump; taking a potion during an even time step decreases the jump. Before taking any potions the cows’ jumping ability is, of course, 0.

No potion can be taken twice, and once the cow has begun taking potions, one potion must be taken during each time step, starting at time 1. One or more potions may be skipped in each turn.

Determine which potions to take to get the highest jump.

输入:

* Line 1: A single integer, P

* Lines 2..P+1: Each line contains a single integer that is the strength of a potion. Line 2 gives the strength of the first potion; line 3 gives the strength of the second potion; and so on.

输出:

* Line 1: A single integer, P

* Lines 2..P+1: Each line contains a single integer that is the strength of a potion. Line 2 gives the strength of the first potion; line 3 gives the strength of the second potion; and so on.

样例输入:

8
7
2
1
8
4
3
5
6

样例输出:

17

/*
分析:
    递推。
    以前见到过这个题,不过没有写出来,这次小比赛倒是写出来了,
是压力的缘故么= =I。
    第一维代表当前读入的第i个东西是被当做偶数干掉、还是按照奇
数被干掉了。第二维的三个代表这个东西是被扔掉了、还是被用来增高
了、还是被用来负增长了。

                                           2013-04-29
*/

#include"iostream"
#include"cstdio"
#include"cstring"
using namespace std;
const int N=155555;
int dp[2][3][N];		//0跳过去,1升、2降
int max(int a,int b){
	return a>b?a:b;
}
int main()
{
	int i,l,j;
	int n,time,temp;
	while(scanf("%d",&n)!=-1)
	{
		time=0;
		memset(dp,0,sizeof(dp));
		for(i=1;i<=n;i++)
		{
			scanf("%d",&temp);
			//不吃
			for(l=0;l<3;l++)
			{
				dp[0][0][i]=max(dp[0][0][i],dp[0][l][i-1]);
				dp[1][0][i]=max(dp[1][0][i],dp[1][l][i-1]);
			}
			//奇数吃升高
			for(l=0;l<3;l++)	dp[1][1][i]=max(dp[1][1][i],dp[0][l][i-1]+temp);
			//偶数吃降低
			for(l=0;l<3;l++)	dp[0][0][i]=max(dp[0][0][i],dp[1][l][i-1]-temp);
		}

		int ans=0;
		for(l=0;l<3;l++)
		for(j=0;j<2;j++)
			if(dp[j][l][n]>ans)	ans=dp[j][l][n];
		cout<<ans<<endl;
	}
	return 0;
}

解题转自:http://blog.csdn.net/ice_crazy/article/details/8867168


  1. 约瑟夫也用说这么长……很成熟的一个问题了,分治的方法解起来o(n)就可以了,有兴趣可以看看具体数学的第一章,关于约瑟夫问题推导出了一系列的结论,很漂亮

  2. 如果两个序列的最后字符不匹配(即X [M-1]!= Y [N-1])
    L(X [0 .. M-1],Y [0 .. N-1])= MAX(L(X [0 .. M-2],Y [0 .. N-1]),L(X [0 .. M-1],Y [0 .. N-1])
    这里写错了吧。