Super Jumping! Jumping! Jumping! (最长子序列)

来源:互联网 发布:网络部咨询员提成方案 编辑:程序博客网 时间:2024/05/21 15:40
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 24 1 2 3 44 3 3 2 10
Sample Output
4103
题意:

有一种新游戏,在一条路径上有起点(start),终点(end),中间点(32位的正整数),

从起点可以跳到多个中间点,再从中间点跳到终点,但是每次跳的都必须比前一个大,

求所有可能中,经过点数字的最大和

思路:

简单的最长子序列问题,只不过 dp记录的是和,最长子序列记录的长度

状态转移方程dp【当且位置i】=max(dp【i之前所有位置】)+val【当前位置的数字】;

ps:之前位置数字均小于当前位置数字

代码:

#include<stdio.h>#include<string.h>#include<algorithm>#define MAX 1005using namespace std;long dp[MAX];//记录走到每一步可以获得的最大值long val[MAX];//记录轨道上的数字int main(){    int n;    while(scanf("%d",&n)!=EOF){        if(n==0) break;        memset(dp,0,sizeof(dp));        memset(val,0,sizeof(val));        int i;        for(i=1;i<=n;i++)            scanf("%ld",&val[i]);        int j;        for(i=1;i<=n;i++){            long sum=0;            int bestj=0;            for(j=1;j<i;j++){ //寻找已经走过的且比当前点小的点中获得的和最大的位置                if(val[j]<val[i]&&dp[j]>sum){                    sum=dp[j];                    bestj=j;                }            }            dp[i]=val[i]+dp[bestj];        }        long maxx=0;        for(i=1;i<=n;i++)            maxx=max(maxx,dp[i]);        printf("%ld\n",maxx);    }return 0;}


阅读全文
0 0
原创粉丝点击