(hdu step 3.2.3)Super Jumping! Jumping! Jumping!(DP:求最长上升子序列的最大和)

在写题解之前给自己打一下广告哈~。。抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下:

http://edu.csdn.net/course/detail/209

题目:

Super Jumping! Jumping! Jumping!

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 896 Accepted Submission(s): 518
 

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


Author

lcy

题目大意:

求最长上升子序列的最大和。

题目分析:

简单DP。求最长上升子序列的最大和。第一层循环用于遍历每一个数,用索引i表示。然后第二层循环用于从头遍历到当前这个数i的前一个数,用索引j表示。如果data[i] > data[j],则说明以索引j结尾的最长上升子序列加上索引i以后依然能构成一个最长上升子序列,但这是依然需要继续向后遍历,因为这是求最长上升子序列的最大和(同一长度的最长上升子序列不唯一)。如果到索引j所形成的最长上升子序列的和大于目前为止的最长上升子序列的和temp,那么就更新temp的值。然后最后通过temp+data[i]便得到了到索引i所能形成的最长上升子序列的最大和。接下来就是更新一下到目前位置所能得到的全局的最长上升子序列的最大和。(上面所说的那个temp只是局部的最长上升子序列)。

至于相求最长上升子序列的长度,可以参考:

http://blog.csdn.net/hjd_love_zzt/article/details/26979313

代码如下:

/*
 * c1.cpp
 *
 *  Created on: 2015年2月9日
 *      Author: Administrator
 */

#include <iostream>
#include <cstdio>

using namespace std;

const int maxn = 1005;
int sum[maxn];
int data[maxn];

int n;

int LIS_SUM(){
	int i;
	int j;

	int maxSum = -9999;
	memset(sum,0,sizeof(sum));

	for(i = 1 ; i <= n ; ++i){
		int temp = 0;
		for(j = 1 ; j < i ; ++j){
			if(data[j] < data[i]){
				if(temp < sum[j]){
					temp = sum[j];
				}
			}
		}

		sum[i] = data[i] + temp;

		if(maxSum < sum[i]){
			maxSum = sum[i];
		}
	}

	return maxSum;
}

int main(){
	while(scanf("%d",&n)!=EOF,n){
		int i;
		for(i = 1 ; i <= n ; ++i){
			scanf("%d",&data[i]);
		}

		printf("%d\n",LIS_SUM());
	}

	return 0;
}
时间: 2024-07-30 05:28:19

(hdu step 3.2.3)Super Jumping! Jumping! Jumping!(DP:求最长上升子序列的最大和)的相关文章

(hdu step 3.2.2)Common Subsequence(简单dp:求最长公共子序列的长度)

在写题解之前给自己打一下广告哈~..抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下: http://edu.csdn.net/course/detail/209 题目: Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 976 Accepted Submission(s): 538   Probl

(hdu step 3.2.6)Monkey and Banana(在第一关键字有序的情况下,根据第二关键字求最长上升子序列的高度之和)

题目: Monkey and Banana Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 720 Accepted Submission(s): 455   Problem Description A group of researchers are designing an experiment to test the IQ of a m

(hdu step 3.2.4)FatMouse&#39;s Speed(在第一关键字升序的情况下,根据第二关键字来求最长下降子序列)

在写题解之前给自己打一下广告哈~..抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下: http://edu.csdn.net/course/detail/209 题目: FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1034 Accepted Submission(s): 526   Proble

hdu 4352 数位dp(最长上升子序列的长度为k的个数)

http://acm.hdu.edu.cn/showproblem.php?pid=4352 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then carefully reading the entire description is very important. As the strongest fighting force in UESTC, xhxj grew

HDU 4513 吉哥系列故事——完美队形II manacher求最长回文

题目来源:吉哥系列故事--完美队形II 题意:中文 思路:在manacher算法向两边扩展的时候加判断 保证非严格递减就行了 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 100110; int a[maxn<<1]; int b[maxn<<1]; int dp[maxn<<1]; int

HDU 1025 Constructing Roads In JGShining&#39;s Kingdom[动态规划/nlogn求最长非递减子序列]

Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 27358    Accepted Submission(s): 7782 Problem Description JGShining's kingdom consists of 2n(n is no mor

(hdu step 3.2.7)免费馅饼(数塔变形:求所能接到馅饼的最大数)

题目: 免费馅饼 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1207 Accepted Submission(s): 508   Problem Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼别处都不掉,就掉落在他身旁的10米

HDU 1243 反恐训练营 (动态规划求最长公共子序列)

反恐训练营 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3040    Accepted Submission(s): 693 Problem Description 当今国际反恐形势很严峻,特别是美国"9.11事件"以后,国际恐怖势力更是有恃无恐,制造了多起骇人听闻的恐怖事件.基于此,各国都十分担心恐怖势力会对本国社会造

HDU 2196 Computer(树形DP求最长路)

Problem Description A school bought the first computer some time ago(so this computer's id is 1). During the recent years the school bought N-1 new computers. Each new computer was connected to one of settled earlier. Managers of school are anxious a