【HDU - 1087】Super Jumping! Jumping! Jumping! (最大上升子序列类问题,dp)
題干:
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 0Sample Output
4 10 3解題報告:
? ? ?就是個最大遞增子序列。
附一個別人的解題報告:
? ? ? 求最大的遞增子序列的和,不用連續(xù),例如(1,5,2),那么(1,2)也算他的遞增子序列。
我剛開始做了一遍,忽略了不用連續(xù)這個問題,結(jié)果發(fā)現(xiàn)好簡單,提交就是不對,應(yīng)當(dāng)注意這里。
這個還是得用動態(tài)規(guī)劃去做,最后的最優(yōu)結(jié)果是由上一步的結(jié)果加上上一次的決策。n個數(shù),由n-1個數(shù)的結(jié)果加上第n個數(shù)。n-1個數(shù)由n-2個數(shù)的結(jié)果。。。。。
推倒最后,前兩個數(shù)的的結(jié)果就很容易求了.以數(shù)列(3,2,4,2,3,6)為例。原博客
?
| 下標(biāo)i | 0 | 1 | 2 | 3 | 4 | 5 |
| a[i] | 3 | 2 | 4 | 2 | 3 | 6 |
| sum[i] | 3 | 2 | 7 | 2 | 5 | 13 |
| ans | 0 | 3 | 7 | 7 | 5 | 13 |
?
?
?
?
AC代碼:
//dp需要賦初值! #include<bits/stdc++.h>using namespace std; int n; int a[1000 + 5]; int dp[1000 + 5]; int main() {while(~scanf("%d",&n)) {if(n == 0) break;memset(dp,0,sizeof(dp));for(int i = 1; i<=n; i++) scanf("%d",&a[i]);for(int i = 1; i<=n; i++) dp[i] = a[i];for(int i = 1; i<=n; i++) {for(int j = 1; j<i; j++) {if(a[i] > a[j] )dp[i] = max(dp[j] + a[i],dp[i] );}}printf("%d\n",*max_element(dp+1,dp+n+1));}return 0 ; }?
總結(jié)
以上是生活随笔為你收集整理的【HDU - 1087】Super Jumping! Jumping! Jumping! (最大上升子序列类问题,dp)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 时而短发英姿飒爽 时而烈焰红唇迷人!宁静
- 下一篇: myfastupdate.exe - m