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.
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.
OutputFor 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 0Sample Output
4 10 3
题意:要从任意起点跳到任意终点,经过的数组和最大,只能从数字小的跳到数字大的
思路:动态规划,最长上升子序列。
状态转移方程:dp【i】=a【i】+max(dp【j】(0<j<i且a【i】>a【j】))
感想:动态规划最长上升子序列即将每一次求dp[i]的值当成求一定要使用第i项的情况下将i当成n的所求的答案值,
而使不使用第i项的情况则通过第i+1项计算时比较dp【i】和dp【0~i】的大小判断第i项是否需要加上,
如dp【1】=dp【0】+a【1】=4,a【1】=-1;dp【0】=5;则dp【2】=maxdp【0~1】+a【i+1】=dp【0】+
a【i+1】;此情况答案就不包含第i项
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int main()
{
int n;
while ((cin>>n)&&n)
{
int a[n];
for (int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
int dp[n];
dp[0]=a[0];
for (int i=1;i<n;i++)
{
int mx=0;
for (int j=0;j<i;j++)
{
if (dp[j]>mx&&a[i]>a[j]) mx=dp[j];
}
dp[i]=a[i]+mx;
}
int mx=0;
for(int i=0;i<n;i++)
mx= max(dp[i],mx);
cout << mx << endl;
}
return 0;
}