題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1087
Super Jumping! Jumping! Jumping!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24452 Accepted Submission(s): 10786
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.

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.
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[i]表示的是前i個並且包含第i個的最大遞增子序列和!給個數據:3 1 4 顯然dp[1]=3,dp[2]=1表示兩個數的最大值。因為分兩種情況討論,如果第二個數大於第一個數,就加上,即dp[2]=dp[1]+num[2];如果不大,dp[2]=num[2];dp[3]=7表示三個數的最大值。首先比較num[3]和num[1],如果num[3]>num[1],dp[3]=7先存下來,如果num[3]>num[2],dp[3]=5依舊存下來;還有一種如果num[3]比前兩個值都小,dp[3]=num[3];最后在存下來的dp[3]中找到一個最大的!呼呼~終於解釋差不多了,小伙伴們也屢一下思路吧0.0
詳見代碼。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 5 using namespace std; 6 7 int num[1010],dp[1010]; 8 9 int main () 10 { 11 int n,Max; 12 while (~scanf("%d",&n)) 13 { 14 if (n==0) 15 break; 16 Max=0; 17 memset(dp,0,sizeof(dp)); 18 for (int i=0;i<n;i++) 19 { 20 scanf("%d",&num[i]); 21 } 22 dp[0]=num[0]; 23 for (int i=1;i<n;i++) 24 { 25 for (int j=0;j<i;j++) 26 { 27 if (num[i]>num[j]) 28 dp[i]=max(dp[i],dp[j]+num[i]); 29 } 30 dp[i]=max(dp[i],num[i]); 31 } 32 for (int i=0;i<n;i++) 33 { 34 Max=max(Max,dp[i]); 35 } 36 printf ("%d\n",Max); 37 } 38 return 0; 39 }