hdu1087 Super Jumping! Jumping! Jumping! (求最大递增子序列和)
2015-11-08 20:44
543 查看
Super Jumping! Jumping! Jumping!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 28955 Accepted Submission(s): 12937
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
Author
lcy
Recommend
We have carefully selected several similar problems for you: 1159 1069 1058 1203 1421
解析:设 f[i] 表示以 i 结尾的最大递增子序列和,则:f[i]=max{f[j]+a[i]} (a[j]<a[i])
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#define ms(a,x,n) memset(a,x,sizeof(a[0])*(n+10))
using namespace std;
const int maxn=1e3;
int a[maxn+10],f[maxn+10];
int main()
{
//freopen("1.in","r",stdin);
int n,i,j,ans;
while(scanf("%d",&n),n)
{
ms(f,0,n);
for(i=1;i<=n;i++)scanf("%d",&a[i]);
for(f[1]=a[1],i=2;i<=n;i++)
{
for(j=1;j<i;j++)
if(a[j]<a[i])f[i]=max(f[i],f[j]);
f[i]+=a[i];
}
for(ans=f[1],i=2;i<=n;i++)ans=max(ans,f[i]);
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- 手把手教你用Execel计算两个矩阵的乘法
- 编程之美-控制CPU占用率曲线方法整理
- hdu 1528-Card Game Cheater(贪心算法)
- iOS中的ARC---内存管理的思考方式
- 为什么工程师需要花时间积累
- MySQL学习笔记(13)之储存引擎
- Android开发获取网络图片时提示java.net.unknownhosteception
- 数据结构之线性表(单链表)
- 选择排序
- [图论]最短路(Dijkstra算法)
- 典型用户
- MySQL学习笔记(12)之触发器(trigger)
- 杭电find the night
- 设计模式 --> (7)外观模式
- C语言运算符优先级
- Java记录 -66- 策略模式
- 浅谈视图的frame、bounds和center属性
- MySQL学习笔记(11)之视图(view)
- HDU 4127Flood-it!
- RBM公式推导