您的位置:首页 > 其它

练习三 Problem C

2016-05-16 17:41 162 查看
题目:

[align=left]Problem Description[/align]
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.<br><br><center><img src=/data/images/1087-1.jpg></center><br><br>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.<br>Your task is to output the maximum value according to the given chessmen list.<br>

[align=left]Input[/align]
Input contains multiple test cases. Each test case is described in a line as follow:<br>N value_1 value_2 …value_N <br>It is guarantied that N is not more than 1000 and all value_i are in the range of 32-int.<br>A test case starting
with 0 terminates the input and this test case is not to be processed.<br>

[align=left]Output[/align]
For each case, print the maximum according to rules, and one line one case.<br>

[align=left]Sample Input[/align]

3 1 3 2
4 1 2 3 4
4 3 3 2 1
0

[align=left]Sample Output[/align]

4
10
3

题目大意:跳棋游戏,给出一些棋子,并赋予其特定的值,要求从最小的一个棋子跳到最大的一个棋子,中间可跳过一个或多个棋子。

解题思路:就是要求一个最大递增子段和,dp方程为dp[j]=max(dp[j],dp[i]+a[j]).

感想:经典dp题型的变形,dp[i]取决于dp[i-1]的最优解,不难做= =。

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
int dp[1010],a[1010];
int max(int c,int d)
{
return c>d?c:d;
}
int main()
{
int Max,i,j,n;
while(cin>>n&&n)
{
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
dp[i]=a[i];
}
Max=a[0];
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
{
if(a[j]>a[i])
dp[j]=max(dp[j],dp[i]+a[j]);
Max=max(Max,dp[j]);
}
printf("%d\n",Max);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: