Super Jumping! Jumping! Jumping! HDU-1087 (最大上升子序列和)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SEVENY_/article/details/83782536

Super Jumping! Jumping! Jumping!

Problem Description

Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now.
 


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.

Input

Input contains multiple test cases. Each test case is described in a line as follow:
N value_1 value_2 …value_N 
It is guarantied that N is not more than 1000 and all value_i are in the range of 32-int.
A test case starting with 0 terminates the input and this test case is not to be processed.

Output

For each case, print the maximum according to rules, and one line one case.

Sample Input

3 1 3 2
4 1 2 3 4
4 3 3 2 1
0

Sample Output

4
10
3

题意:

给出很多个测试样例,每行输入一个n以及n个数,当n=0时,结束。要从起点跳到终点,中间会有n个棋子,每个棋子都有数值,每个人都必须从一个棋子跳到另一个值更大的棋子。求跳跃路径中棋子的价值的总和。

正确思路:

这个题其实就是要求最大递增序列的和。序列一定是要递增的,而且是要和最大,不是说序列长度要最长!!

正确代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int main()
{
	int n,a[1009];  //n个棋子,a数组存放n个棋子的价值
	int dp[1009];   //存放i之前的最大递增序列的和(最大的和)
	int ans=0;      //存放结果,最大的棋子的价值
	while(true)
	{ 
	    ans=0;
	    memset(dp,0,sizeof(dp)); 
		scanf("%d",&n);
		if(n==0)break;
		for(int i=0;i<n;i++)
		{
			scanf("%d",&a[i]); 
		} 
		for(int i=0;i<n;i++)
		{
			dp[i]=a[i];
			for(int j=0;j<i;j++)
			{
				if(a[j]<a[i])  //如果这个棋子比前面的棋子价值大
				{
					dp[i]=max(dp[i],dp[j]+a[i]);  //更新i之前跳到的棋子的最大的和
				}
			} 
			ans=max(ans,dp[i]);
		}  
		printf("%d\n",ans);
	}
	return 0;
} 

错误思路:(别看)

开始我的思路就觉得这不就是求最长子序列吗,然后再求和。并不是!!

错误代码:(别看)

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int main()
{
	int n,a[1009];  //n个数,a数组来存放n个棋子的值
	int dp[1009];   //用来求最长上升子序列
	int sum[1009];  //每个子序列的价值总和
	int ind=0,ans=0;//ind个子序列   ans代表所求的最大的棋子总和
	while(true)
	{ 
	    ind=0;ans=0;
	    memset(dp,0,sizeof(dp));
	    memset(sum,0,sizeof(sum));
		scanf("%d",&n);
		if(n==0)break;
		for(int i=0;i<n;i++)
		{
			scanf("%d",&a[i]); 
		}
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<i;j++)
			{
				if(a[j]>=a[i])
				{
					dp[i]=max(dp[i],dp[j]+1); //最长上升子序列不一定总和就是最大的!! 
				}
			}
			ind=max(ind,dp[i]);  //找到子序列的个数
			sum[dp[i]]+=a[i];   
		}
		for(int i=0;i<=ind;i++)
		  ans=max(ans,sum[i]);
		printf("%d\n",ans);
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/SEVENY_/article/details/83782536