HDU1087:Super Jumping!(DP)

Super Jumping! Jumping! Jumping!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 53137 Accepted Submission(s): 24629

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

題目大意是棋子只能向右跳,而且只能跳到比自己數值大的位置上。

我們可以創個dp[i]數組來表示:(最後一個數是a[i]的上升子序列)的最大的SUM。

思路大概是這樣:爲了偷懶,將輸入的數存入1~n而不是0 ~ n-1的a數組中,這樣比較好看一點。然後每個數字可以讓自己成爲一個子序列,這個數字的值就是dp[i]的最小值,所以一開始輸入的時候就可以做兩個步驟:

  1. cin >> a[i];
  2. dp[i] = a[i];

又因爲dp[i]的意思是(最後一個數是a[i]的上升子序列)的最大的SUM。所以設一個變量 j 從1一直遍歷到(n-1),如果a[i] > a[j], 那麼就將a[i] + dp[ J ] 和 dp[ i ]相比較,若比dp[i]大,就把dp[i]給覆蓋掉,代碼實現爲dp[i] = max(dp[i],a[i] + dp[j]); 。然後在每個內層循環結束後,也就是dp[i]確定後將他和之前的最大值相比較,若比最大值大就把他覆蓋掉,temp = max(temp,dp[i]); ,因爲我直接從dp[2]開始逐步填數進去,所以我的temp(最大值)一開始便賦值爲dp[1]。
最後輸出temp即可。

最後附上ac代碼:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <ctype.h>
#include <queue>
#include <cmath>
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;
typedef long long int ll;
int a[2000];
int dp[2000];
int main() {
    int n;
    while(cin >> n) {
        if(n == 0) {break;}
        for(int i = 1; i <= n; i++) {
            cin >> a[i];
            dp[i] = a[i];
        }
        int temp = dp[1]; //暫存最大值
        for(int i = 2; i <= n; i++) {
            for(int j = 1; j < i; j++) {
                if(a[j] < a[i]) {
                    dp[i] = max(dp[i],dp[j] + a[i]);
                }
            }
            temp = max(temp,dp[i]);
        }
        cout << temp << endl;
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章