HDU-1087Super Jumping! Jumping! Jumping!

来源:互联网 发布:影响淘宝关键词 编辑:程序博客网 时间:2024/05/13 01:20

Super Jumping! Jumping! Jumping!

                                                                                                             Time Limit: 2000/1000 MS (Java/Others)   

                                                                                                            Memory Limit: 65536/32768 K (Java/Others)


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 24 1 2 3 44 3 3 2 10
 

Sample Output
4103

      题目意思很好懂,但所给的测试样例太水(hdu上的样例都是这样),以为要用最长单调递增子序列,但一直没有好的思路,,后来TJY小田想出了一个很好的思路;就是用两层循环,一层输入,一层查询,只要输入的数据其前面的数据满足条件,就直接用另外一个数组相加储存最大和,那么,,需要满足什么条件呢:

   请看样例:

  4  1 2 3 4;输出10,1前面没有谁比他小,故1的位置就是1,而2前面有1,故2的位置是3,同理3的位置是6,4的位置是10,看出来了吧,前面的数据小就加起来;

  再看这组数据:
  5 1 3 2 5 6 ;输出是15,这组应该没什么问题;

  6  4 9 6 8 5 10;输出28,来分析看,9的位置是13,而6的位置是10,8的位置是18,5的位置是9,10的位置是28;

只要前面的数据比当前数据小就加起来;


但  7 4 9 2 6 8 5 10;输出应该是28,用刚刚的方法就不行了,因为2这个数据很小,后面的数据都会加上它 ,而实际上它是不算在递增序列中的,所以,,看代码:
    

#include<cstdio>#include<algorithm>#include<iostream>#include<cstring>#include<cmath>using namespace std;const int N=1000+10;int a[N],c[N];int main(){    int n,i,j,k;    while(scanf("%d",&n)&&n)    {        memset(c,0,sizeof(c));        for(i=1; i<=n; i++)        {            scanf("%d",&a[i]);            c[i]=a[i],k=0;            for(j=1; j<i; j++)            {                if(a[j]<a[i])               c[i]=max(c[i],a[i]+c[j]);//手推上面的样例就会明白的;                }        }        sort(c+1,c+n+1);        printf("%d\n",c[n]);    }    return 0;}


0 0
原创粉丝点击