动态规划 ② HDU - 1087 Super Jumping! Jumping! Jumping! (LIS 最大上升子序列)

问题重述:


The game can be played by two or more than two players. It consists of a chessboard(棋盘)and some chessmen(棋子), and all chessmen are marked by a positive integer or “start” or “end”. The player starts from start-point and must jumps into end-point finally. In the course of jumping, the player will visit the chessmen in the path, but everyone must jumps from one chessman to another absolutely bigger (you can assume start-point is a minimum and end-point is a maximum.). And all players cannot go backwards. One jumping can go from a chessman to next, also can go across many chessmen, and even you can straightly get to end-point from start-point. Of course you get zero point in this situation. A player is a winner if and only if he can get a bigger score according to his jumping solution. Note that your score comes from the sum of value on the chessmen in you jumping path. 
Your task is to output the maximum value according to the given chessmen list.

总结出来题目的叙述就是这么几点:1. 只能向前跳;2. 下一个数必须大于前一个数;3. 找序列和的最大值。

这道题非常清晰,典型的 LIS(Longest Increasing Sequence 最大上升子序列),结果的特点是序列和最大且单调递增。

问题求解:

将dp[n]记作以棋子n为起点的LIS值,dp[i]为前一状态的LIS值,dp[j]为下一状态的LIS值,状态转移方程为 dp[j] = max { dp[j] , dp[i] + a[j] },a[j]是第j个棋子的weight。注意这个转移方程只有在a[j] > a[i]的时候才更新数值,根据题干的1和2两点容易说明,最后别忘了更新要输出的最大值maximum。

形象地去解释这道题的思路就是,站在一个棋子向前看,如果有比我当前棋子weight大的棋子就往下跳,直到跳到终点为止。最后从各个情况中选出最大值。

AC代码:

#include
//#include
using namespace std;
#include

_int32 max(_int32 x, _int32 y)
{
	return x > y ? x : y;
}

int main()
{
	int n;
	while (cin >> n, n)
	{
			_int32 *arr = new _int32[n];
			_int32 dp[1001];
			for (int i = 0; i < n; i++)
			{
				cin >> arr[i];
				dp[i] = arr[i];
			}
			_int32 maxi = dp[0];
			for (int i = 0; i < n - 1; i++)
			{
				for (int j = i + 1; j < n; j++)
				{
					if (arr[j] > arr[i])
					{
						dp[j] = max(dp[j], dp[i] + arr[j]);
						maxi = max(maxi, dp[j]);
					}
				}
			}
			cout << maxi << endl;
	}
	//system("pause");
	return 0;
}


你可能感兴趣的:(ACM_Beginner,Learning,Notes)