HDU - 1087 Super Jumping! Jumping! Jumping!

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

一道很典型的DP,题目的特点在所选择的序列中,后一个数要严格大于前一个数。

对于这种DP,我的一种思路就是“多线程”的思想,因为如果只用一个序列的话,很有可能“一叶障目,不见泰山”。多线程可以避免掉这一不足,但是有可能会导致超时。

回到题目本身,dp[i]代表的是前i项(包括第i项)最大的和,于是我就让con[i]和前面所有的(con[i]>con[j],j>=1&&j<i)dp[j]相加,取最大的那个和。

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #include<math.h>
 5 #include<algorithm>
 6 #include<queue>
 7 #include<stack>
 8 #include<deque>
 9 #include<iostream>
10 using namespace std;
11 typedef long long  LL;
12 int con[1009];
13 int dp[1009];
14 int main()
15 {
16     LL i,p,j,n,t;
17     LL mid,head;
18     while(scanf("%lld",&n)!=EOF)
19     {
20         if(n==0)
21             break;
22         for(i=1;i<=n;i++)
23             scanf("%d",&con[i]);
24         memset(dp,0,sizeof(dp));
25         dp[1]=con[1];
26         for(i=2;i<=n;i++)
27         {
28             head=-1;
29             for(j=1;j<i;j++)
30             {
31                 if(con[i]>con[j])
32                 {
33                     mid=dp[j]+con[i];
34                     if(mid>head)
35                         head=mid;
36                 }
37             }
38             if(head==-1)
39                 dp[i]=con[i];
40             else
41                 dp[i]=head;
42         }
43         head=-1;
44         for(i=1;i<=n;i++)
45             if(dp[i]>head)
46                 head=dp[i];
47         printf("%lld\n",head);
48     }
49     return 0;
50 }
View Code

猜你喜欢

转载自www.cnblogs.com/daybreaking/p/9384592.html