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;
}


猜你喜欢

转载自blog.csdn.net/qq_43555854/article/details/86644210