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;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章