Super Jumping! Jumping! Jumping! |
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) |
Total Submission(s): 770 Accepted Submission(s): 454 |
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
|
Sample Output 4
|
来自 <http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=3§ionid=2&problemid=3>
在做这一题之前,我做的是CommomSubsequence这一道题,两者有相似之处,所以这题便轻易的做出来了
题意:超级跳跳跳,只能从起点跳到终点,而且只能按照数字递增的跳跃,且不能回头,如第一个实例中,
可以: 起点 -> 1 -> 3 -> 终点,不能:起点 -> 1-> 3 -> 2 ->终点这么跳(2 < 3)。
分析:
求输入第n个数的最大权值,可以找前面比n个数小的数相加起来而得到,如第二个实例,在第3个数3时,最大值已经是1+2+3 = 6,当输入4时,直接4+6就是最大的了,不用再重复加,这就是动态规划的好处。当输入为1 3 7 4 2 8 时, 8既要考虑倒数第二的最大值,还要考虑倒数第三,第四的,这种情况怎么办呢?大家自己想想哈~。
代码如下:
#include<stdio.h>
const int MAX =1000;
long longnumber[MAX];
long long max[MAX];
int main()
{
inthowNum,Max = 0;
while(scanf("%d",&howNum))
{
if(!howNum)break;
scanf("%d",&number[0]);
max[0]= number[0];
Max= number[0];
for(inti = 1; i < howNum; i++)
{
scanf("%d",&number[i]);
max[i]= number[i];
intj = i;
inttemp = -3,tempMax = 0;
while(--j> -1)
{
if(number[i]<= number[j]) continue;
if(number[j]<= temp) continue;
tempMax= tempMax < max[j] ? max[j] : tempMax;
temp= number[j];
}
max[i]+= tempMax;
Max= Max < max[i] ? max[i] : Max;
}
printf("%d\n",Max);
}
}
下面代码是在别人博客上拷下来的,代码风格很漂亮,传递一下,建议大家都设计一套具有个性又漂亮的代码风格~~~编程也是一个创造美的过程呀!