动态规划:Super Jumping! Jumping! Jumping!

来源:互联网 发布:hashmap的hash算法 编辑:程序博客网 时间:2024/06/05 06:19
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 1

0

思路:

这道题是要求最大非连续递增数字和,采用的是一种动规的思想,dp[i]保留的是前i项的最大和,然而,这道题

做起来就是那么简单,就像最长单调递增子序列,比较一下大小,加上限制条件,举个例子吧

样例: 4 1 4 3 5 输出结果是10而不是9;当然,这傻子也看得出来,这时候,你就要想一下刚才你是怎么

瞬间把他们比较出来的,因为在第四项,5+1+4>5+1+3,这时候,这道题的思想就出来了,

如果dp[i]>dp[j]+a[i](j永远小于i),那就不用更新dp[i]的值,相反,需要更新,因为dp[i]保留的是前i项

的最大和。

因为本博主才学尚浅,所以在下对动规的理解是,只要你找到了其中一步,再加上初始化,写出动态转移方程

,基本上就可以解决了。(勿喷小弟我)。。。

代码:

#include<stdio.h> int a[1003],dp[1004];int main(){    int n;    while(~scanf("%d",&n)&&n)    {        for(int i=0; i<n; i++)        {            scanf("%d",&a[i]);            dp[i]=a[i];//给dp[i]赋初值        }        int maxx=a[0];        for(int i=0;i<n;i++)        {            for(int j=0;j<i;j++)            {                if(a[i]>a[j]&&dp[j]+a[i]>dp[i])                    dp[i]=dp[j]+s[i];            }            if(dp[i]>maxx)                maxx=dp[i];        }        printf("%d\n",maxx);    }}



0 0
原创粉丝点击