HDU1087:Super Jumping! Jumping! Jumping!(DP水題)

Super Jumping! Jumping! Jumping!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 63733    Accepted Submission(s): 29669


ios

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
 

 

Author
lcy
 
 
題意:
一條直線上有N個點,每一個點有對應值,從最左邊的起點(視爲0)開始,跳到右邊值大於它的點上,而後將值更新爲跳到的點的值,問跳到終點(視爲無限大)所通過的值的和最大爲多少。
題解:
由題目最多隻有一千個點能夠聯想到直接DP便可,每一個點枚舉前面全部的點找出能跳到它的點的和最大的點(我在說什麼……)。即用dp[N]記錄每一個點的最大的和。具體見下代碼;
#define _CRT_SECURE_NO_DepRECATE
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
#include <algorithm>
#include <bitset>
#include <cstdlib>
#include <cctype>
#include <iterator>
#include <vector>
#include <cstring>
#include <cassert>
#include <map>
#include <queue>
#include <set>
#include <stack>
#define ll long long
#define INF 0x3f3f3f3f
#define ld long double
const ld pi = acos(-1.0L), eps = 1e-8;
int qx[4] = { 0,0,1,-1 }, qy[4] = { 1,-1,0,0 }, qxx[2] = { 1,-1 }, qyy[2] = { 1,-1 };
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n, num[1001], dp[1001], maxx;
    while (cin >> n && n)
    {
        memset(dp, 0, sizeof(dp));
        for (int i = 0; i < n; i++)
        {
            cin >> num[i];
        }
        for (int i = 0; i < n; i++)
        {
            maxx = 0;
            for (int f = 0; f < i; f++)//遍歷找出能跳到i的最大的值
            {
                if (num[i] > num[f])
                {
                    maxx = max(dp[f], maxx);
                }
            }
            dp[i] = maxx + num[i];
        }
        for (int i = 0; i < n; i++)
        {
            maxx = max(maxx, dp[i]);
        }
        cout << maxx << endl;
    }
    return 0;
}
相關文章
相關標籤/搜索