动态规划-3003-序列的最大上升子序列

来源:互联网 发布:淘宝网店实名认证照片 编辑:程序博客网 时间:2024/03/28 21:52

Problem C

Time Limit : 2000/1000ms (Java/Other)   Memory Limit :65536/32768K (Java/Other)

Total Submission(s) : 17   Accepted Submission(s) : 3

Problem Description

Nowadays, a kindof 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 toyou now.<br><br><center><img src=/data/images/1087-1.jpg></center><br><br>Thegame 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 positiveinteger or “start” or “end”. The player starts from start-point and must jumpsinto end-point finally. In the course of jumping, the player will visit thechessmen in the path, but everyone must jumps from one chessman to anotherabsolutely bigger (you can assume start-point is a minimum and end-point is amaximum.). And all players cannot go backwards. One jumping can go from achessman to next, also can go across many chessmen, and even you can straightlyget to end-point from start-point. Of course you get zero point in thissituation. A player is a winner if and only if he can get a bigger scoreaccording to his jumping solution. Note that your score comes from the sum ofvalue on the chessmen in you jumping path.<br>Your task is to output themaximum value according to the given chessmen list.<br>

 

 

Input

Input containsmultiple test cases. Each test case is described in a line asfollow:<br>N value_1 value_2 …value_N <br>It is guarantied that Nis not more than 1000 and all value_i are in the range of 32-int.<br>Atest case starting with 0 terminates the input and this test case is not to beprocessed.<br>

 

 

Output

For each case,print the maximum according to rules, and one line one case.<br>

 

 

Sample Input

3 1 3 2

4 1 2 3 4

4 3 3 2 1

0

 

 

Sample Output

4

10

3

题意:求序列的最大上升子序列

思路:每个位置都有一个最大和,在此第n个位置的前面n-1个位置中选最接近a[n]且要小于a[n]的那个位置的最大和再加上a[n],就得到第n个位置的最大和,然后再找出所以位置中的最大和,就是所要的答案,小于an[j]的位置和sum[i]来算出本位置最大和 temp+an[i]。

代码:

#include<cstdio>
#include<cstring>
#define Maxn 10001
#include<cmath>
#include<algorithm>


using namespace std;


int n,an[1010];
int sum[1002];
int main(){
int i,j,temp,maxn;
while(scanf("%d",&n),n){
memset(sum,0,sizeof(sum));
memset(an,0,sizeof(an));
for(i=1;i<=n;i++)
scanf("%d",&an[i]);
maxn=-1;
for(i=1;i<=n;i++){
temp=0;
for(j=i-1;j>0;j--)


if(an[j]<an[i]&&temp<sum[j]) temp=sum[j];
sum[i]+=an[i]+temp;
maxn=max(maxn,sum[i]);
}
printf("%d\n",maxn);
}
return 0;
}

0 0