(Hdu step 3.2.3) Super Jumping! Jumping! Jumping! (DP: Calculate the largest sum of the longest ascending subsequence)
Question:
Super Jumping! Jumping! Jumping! |
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) |
Total Submission (s): 896 Accepted Submission (s): 518 |
|
Problem DescriptionNowadays, 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 into SS unzip 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. |
InputInput 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. |
OutputFor each case, print the maximum according to rules, and one line one case. |
Sample Input3 1 3 24 1 2 3 44 3 3 2 10 |
Sample Output4103 |
Authorlcy |
Question:
Calculate the largest sum of the longest ascending subsequence.
Question Analysis:
Simple DP. Calculate the largest sum of the longest ascending subsequence. The first loop is used to traverse each number, represented by index I. Then the second-layer loop is used to traverse the previous number of I from the beginning to the current number, represented by index j. If data [I]> data [j], it means that the longest ascending subsequence ending with index j Plus index I can still constitute the longest ascending subsequence, however, this still requires backward traversal, because this is the largest sum of the longest ascending subsequence (the longest ascending subsequence with the same length is not unique ). If the sum of the longest ascending subsequences formed by index j is greater than the sum of the longest ascending subsequences and temp so far, the value of temp is updated. Finally, we can use temp + data [I] to obtain the largest sum of the longest ascending subsequence formed by index I. Next, we will update the largest sum of the global longest ascending subsequence that can be obtained from the current position. (The temp mentioned above is only the longest ascending subsequence of a local part ).
For the length of the longest ascending subsequence, see:
The Code is as follows:
/** C1.cpp ** Created on: February 9, 2015 * Author: Administrator */# include
# Include
Using namespace std; const int maxn = 1005; int sum [maxn]; int data [maxn]; int n; int LIS_SUM () {int I; int j; int maxSum =-9999; memset (sum, 0, sizeof (sum); for (I = 1; I <= n; ++ I) {int temp = 0; for (j = 1; j <I; ++ j) {if (data [j] <data [I]) {if (temp <sum [j]) {temp = sum [j] ;}}sum [I] = data [I] + temp; if (maxSum <sum [I]) {maxSum = sum [I] ;}return maxSum ;}int main () {while (scanf (% d, & n )! = EOF, n) {int I; for (I = 1; I <= n; ++ I) {scanf (% d, & data [I]);} printf (% d, LIS_SUM ();} return 0 ;}