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.

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 Input

3 1 3 2
4 1 2 3 4
4 3 3 2 1
0

Sample Output

4
10
3
題意:在start->end這條路上有多個棋手,每個棋手都有一個價值,如果你想獲得某個棋手的價值則該棋手的價值必須比上一個獲得的棋手的價值大,求在這條路線上你能獲得的最大價值
分析:從題面上來看,是讓我們求最大遞增子序列的和。如果我們要求前k項max(lIs),那我們可以從前k項遍歷,如果str[j]<str[k],則dp[k]=max(dp[k],dp[j]+str[k]),反之我們不更新。
dp[i]表示前i項最大遞增子序列的和

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 #include<stack>
 5 #include<queue>
 6 #include<iostream>
 7 #include<map>
 8 #include<vector>
 9 #define Inf 0x3f3f3f3f
10 #define PI acos(-1.0)
11 using namespace std;
12 int dp[1234];
13 int str[1234];
14 int main()
15 {
16     int m,n,i,j,pos;
17     while(scanf("%d",&m)!=-1&&m)
18     {
19     for(i=1; i<=m; i++)
20     {
21         scanf("%d",&str[i]);
22     }
23      memset(dp,0,sizeof(dp));
24      int ans=-Inf;
25     for(i=1;i<=m;i++)
26     {
27         dp[i]=str[i];
28         for(j=1;j<=i;j++)
29         {
30             if(str[j]<str[i])
31             {
32                 dp[i]=max(dp[i],dp[j]+str[i]);
33             }
34         }
35         ans=max(ans,dp[i]);
36 
37     }
38     cout<<ans<<endl;
39     }
40     return 0;
41 }
View Code

 

我們會發現對與前n項的max(LIS),都有這個重疊子問題,因此
我們構造狀態轉移方程dp[k]=max(dp[k],dp[j]+str[k])



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM