HDU——1087 Super Jumping!Jumping!Jumping!(dp 遞增子序列最大和)

Problem Description

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

題目大意:

給定一串數字 , 求出其一個遞增子序列,並使其和最大。
注:子序列與子串的區別:子序列不一定是連續的 , 但是子串一定是連續的。
  例如:給定一個數串 :1 3 2 4 6 2
     其遞增子序列的最大和爲 (1 + 3 + 4 + 6) = 14

解題思路:

利用數組dp[i] 來記錄到第 i (包含i)個數字的遞增子序列和。
繼續以上述例子爲例:num : 1 3 2 4 6 2 (下標從0開始)
  顯然,dp[0] = num[0] = 1;
  求dp[1] 時 ,num[1] 之前只有一個 num[0]
        比較num[0] 和 num[1] 的大小 , 若num[0] > num[1] , 則 dp[1] = num[1]
        若num[0] < num[1], 符合遞增條件,則dp[1] = dp[0] + num[1];
  求dp[2]時 , 依次比較 num[2] 與 num[0]、num[1]的關係;
           首先 num[2] 和 num[0] 比較, 若num[0] > num[2] , 則 dp[2] = num[2]
          若num[0] < num[2], 符合遞增條件,則dp[2] = dp[0] + num[2];
        注:這裏的dp[2]都只是一個暫存值。
           再比較num[2] 和 num[1] , 若num[1] > num[2] , 則 dp[2] = num[2]
           若num[1] < num[2], 符合遞增條件,則dp[2] = dp[1] + num[2];
        注:上面之所以說dp[2]是暫存值是因爲,num[2]之前有兩個數,
          這裏他就有可能有多個不同的值,
          根據題目要求dp[2]取多個值中的最大值。
  接下來,dp[3] ….dp[5]也都這樣計算。

代碼:

#include <cstdio>
#include <cstring>
using namespace std;

int path[1010];
int dp[1010];
int main(){
    int n , i , j , maxsum = 0;
    while(scanf("%d",&n) != EOF){
        if( n == 0)
            break;
        maxsum = 0;
        memset(path , 0 , sizeof(path));
        memset(dp , 0 , sizeof(dp));
        for(i = 0 ; i < n ; i ++)
            scanf("%d",&path[i]);

        dp[0] = path[0];
        maxsum = dp[0];
        for(i = 1 ; i < n ; i ++){
            for(j = 0 ; j < i ; j ++){
                if(path[i] > path[j])
                    dp[i] = dp[i] > dp[j] + path[i] ? dp[i] : dp[j] + path[i];
                if(path[i] < path[j])
                    dp[i] = dp[i] >  path[i] ? dp[i] : path[i];
            }
            if(dp[i] > maxsum)
                maxsum = dp[i];
        }

        printf("%d\n" , maxsum);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章