HDUOJ 1087 - SuperJumping!Jumping!Jumping!(DP:最大递增子序列值之和【类似LIS】)

#原题

  • Problem Description

这里写图片描述
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.

  • 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

解题思路:

有点类似求最长递增子序列(LIS),但这道题的要求不仅要递增,还需要累加的值的最大的。
可参考:https://www.cnblogs.com/sasuke-/p/5396843.html

代码:

最大递增子序列值之和
#include<stdio.h>
#include <algorithm>
using namespace std;
int dp[1001], x[1001];//dp[i]为从0位置到第i位置的最大递增子序列值之和
int main()
{
	int N, i, j, MAX;
	while (scanf("%d",&N)!=EOF&&N!=0)
	{
		memset(dp, 0, sizeof(dp));
		memset(x, 0, sizeof(x));
		MAX = 0;
		for(i = 1; i <= N; i++)
		{
			scanf("%d", x + i);
			for (j = 1; j < i; j++)//利用递推的原理
				if (x[i] > x[j])//如果该值(x[i])比之前的其他值(x[j])大
					dp[i] = max(dp[j] + x[i], dp[i]);//由前面的最大递增子序值之和,推出后面的最大递增子序值之和
			if (dp[i] == 0)//如果该值(x[i])比之前的其他值(x[j])都小【即没有被赋值】
				dp[i] = x[i];
			MAX = MAX > dp[i] ? MAX : dp[i];//寻找最大的值
		}
		printf("%d\n", MAX);
	}
}
最长递增子序列(LIS)
#include<cstdio>
#include<cmath>
#include<cstring>
const int qq=1005;
int dp[qq];
int num[qq]; 
int main()
{
    int n;
    while(~scanf("%d",&n)){
        memset(dp,0,sizeof(dp));
        for(int i=0;i<n;++i)
        scanf("%d",&num[i]);
        dp[0]=1;
        int x=0;
        for(int j,i=0;i<n;++i){
            int maxn=0;
            for(j=0;j<i;++j)    //这里利用了递推的原理、 
                if(num[i]>num[j])    //由前面的最长递增子序列推出后面的最长递增子序列、 
                    maxn=maxn>dp[j]?maxn:dp[j];
            dp[i]=maxn+1;
            if(dp[i]>x)    x=dp[i];    //x记录的是当前的最大值、 
        }
        printf("%d\n",x);
    }
    return 0;
}

对LIS的nlogn优化算法可参考:http://blog.csdn.net/Ming991301630/article/details/78596090

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值