HDU1087 Super Jumping! Jumping! Jumping!

Super Jumping! Jumping! Jumping!

problem

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

題意

給定一個序列,找出一個子序列(不必連續),使得這個子序列的和最大,輸出這個和。

分析

一開始以爲是求最長上升子序列(LIS),後來發現不是,這個是要求出和最大,而LIS是求最長,長度最長的和不一定大,例如 100,1,200,3,4,5,6,最長的上升子序列是 1 3 4 5 6,而和最大的上升子序列是 100 200。
因爲這題數據小,序列長度不超過1000,所以可以用 O(n2)的dp。
定義 dp[i] 爲前 i 個數裏最大上升子序列的和(且包含a[i], 也就是 a[i]是這個子序列裏的最大值)
轉移方程:dp[i] = max(dp[k], 0) + a[i], (1 <= k < i, a[i] > a[k])
dp[1]、dp[2]、... dp[k] ... 、dp[i-1]中篩選出 a[i] > a[k] 的那些 dp[k],然後再挑出最大值,暫記爲 tempmax, 如果不存在這樣的 dp[k],也就是前面那些都比 a[i] 大,tempmax = 0,

計算 dp[i] 時,就是把 a[i] 貼在 dp[1]、dp[2]、... dp[k] ... 、dp[i-1] 其中一個後面,此時挑一個最大的貼,如果全是負數,那就不貼,只留a[i]自己。最終結果就是從 dp[] 裏找個最大值。

#include<bits/stdc++.h>
using namespace std;
typedef  long long LL; 
const int maxn = 100005;
LL a[100005];
LL dp[100005];
int main() {
    std::ios::sync_with_stdio(false);
    int n;
    while(cin >> n, n) {
        for(int  i = 1; i <= n; i++) {
            cin >> a[i];
        }
        // 定義 dp[i] 爲前 i 個數裏最大上升子序列的和(且包含a[i], 也就是 a[i]最大) 
        // dp[i] = max(dp[k], 0) + a[i], (1 <= k < i, a[i] > a[k])
        for(int i=1;i<=n;i++){
            LL tempmax = 0;
            for(int k=1; k < i;k++){
                if(a[i] > a[k]){
                    tempmax = max(tempmax, dp[k]);
                }
            } 
            dp[i] = tempmax + a[i];
        }
        LL sum = -0x3fffffff;
        for(int i=1;i<=n;i++){
            sum = max(sum, dp[i]);
        }
        cout << sum << endl;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章