您的位置:首页 > 其它

hdu-1087 Super Jumping! Jumping! Jumping!(DP解法)

2016-12-20 17:14 369 查看

hdu-1087 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): 35239 Accepted Submission(s): 16017

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]为以第i个元素结尾的递增子数组的最大子数组和,则状态方程可写作:dp[i] = MAX(dp[i], dp[j] + arr[i]),其中方程的约束条件为0 <= j <= i && arr[j] < arr[i],约束条件的理解应该是,在i位置之前,寻找使得arr[j] < arr[i]成立的位置,并将其对于dp值与dp[i]做比较,取较大的值,但不应该仅找一个位置就结束,应该寻找到i-1的位置。具体代码如下:

Code

//
//  main.cpp
//  hdu1087
//
//  Created by Morris on 2016/12/20.
//  Copyright © 2016年 Morris. All rights reserved.
//

#include <cstdio>
#include <cstring>
#include <algorithm>

#define MAX(m, n) ( (m > n) ? m : n )

namespace {
using std::scanf;
using std::printf;
using std::memset;
}

const int g_kMaxSize = 1001;

int main(int argc, const char *argv[])
{
int n;
int arr[g_kMaxSize] = { 0 };
int dp[g_kMaxSize] = { 0 };

int i, j;
int res;
while (~scanf("%d", &n)) {
if (!n) {
break;
}

for (i = 0; i < n; ++i) {
scanf("%d", &arr[i]);
}

dp[0] = arr[0];
for (i = 1; i < n; ++i) {
dp[i] = arr[i];
for (j = 0; j < i; ++j) {  //在i位置之前寻找符合条件的j
if (arr[j] < arr[i]) {
dp[i] = MAX(dp[i], dp[j] + arr[i]);
}
}
}

//在dp数组中寻找最后需要求解的最大递增子数组和
res = -1;
for (i = 0; i < n; ++i) {
res = MAX(res, dp[i]);
}

printf("%d\n", res);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: