Super Jumping! Jumping! Jumping(最大递增子序列的和)
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.
InputInput 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.
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
0
Sample Output
4
10
3
题意:在start->end这条路上有多个棋手,每个棋手都有一个价值,如果你想获得某个棋手的价值则该棋手的价值必须比上一个获得的棋手的价值大,求在这条路线上你能获得的最大价值
分析:从题面上来看,是让我们求最大递增子序列的和。如果我们要求前k项max(lIs),那我们可以从前k项遍历,如果str[j]<str[k],则dp[k]=max(dp[k],dp[j]+str[k]),反之我们不更新。
dp[i]表示前i项最大递增子序列的和
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<iostream>
#include<map>
#include<vector>
#define Inf 0x3f3f3f3f
#define PI acos(-1.0)
using namespace std;
int dp[];
int str[];
int main()
{
int m,n,i,j,pos;
while(scanf("%d",&m)!=-&&m)
{
for(i=; i<=m; i++)
{
scanf("%d",&str[i]);
}
memset(dp,,sizeof(dp));
int ans=-Inf;
for(i=;i<=m;i++)
{
dp[i]=str[i];
for(j=;j<=i;j++)
{
if(str[j]<str[i])
{
dp[i]=max(dp[i],dp[j]+str[i]);
}
}
ans=max(ans,dp[i]); }
cout<<ans<<endl;
}
return ;
}
我们会发现对与前n项的max(LIS),都有这个重叠子问题,因此
我们构造状态转移方程dp[k]=max(dp[k],dp[j]+str[k])
Super Jumping! Jumping! Jumping(最大递增子序列的和)的更多相关文章
- HDU 1087 Super Jumping! Jumping! Jumping! 最大递增子序列
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- HDU 1087 简单dp,求递增子序列使和最大
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- (转载)最长递增子序列 O(NlogN)算法
原博文:传送门 最长递增子序列(Longest Increasing Subsequence) 下面我们简记为 LIS. 定义d[k]:长度为k的上升子序列的最末元素,若有多个长度为k的上升子序列,则 ...
- nyoj17_又做最大递增子序列
单调递增最长子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 求一个字符串的最长递增子序列的长度 如:dabdbf最长递增子序列就是abdf,长度为4 输入 ...
- 最长公共子序列(LCS)和最长递增子序列(LIS)的求解
一.最长公共子序列 经典的动态规划问题,大概的陈述如下: 给定两个序列a1,a2,a3,a4,a5,a6......和b1,b2,b3,b4,b5,b6.......,要求这样的序列使得c同时是这两个 ...
- 最长递增子序列 O(NlogN)算法
转自:点击打开链接 最长递增子序列,Longest Increasing Subsequence 下面我们简记为 LIS. 排序+LCS算法 以及 DP算法就忽略了,这两个太容易理解了. 假设存在一个 ...
- [LintCode] Longest Increasing Continuous Subsequence 最长连续递增子序列
Give an integer array,find the longest increasing continuous subsequence in this array. An increasin ...
- 51nod 1134 最长递增子序列
题目链接:51nod 1134 最长递增子序列 #include<cstdio> #include<cstring> #include<algorithm> usi ...
- 动态规划 - 最长递增子序列(LIS)
最长递增子序列是动态规划中经典的问题,详细如下: 在一个已知的序列{a1,a2,...,an}中,取出若干数组组成新的序列{ai1,ai2,...,aim},其中下标i1,i2,...,im保持递增, ...
随机推荐
- 在一个Activity中启动另一个Activity
一.新建一个空的工程 二.添加一个Activity并命名为BAty 三.在activity_main.xml中添加一个按钮,设置id号为btnStartB <Button android:lay ...
- NHibernate的几种查询方式
SQL:普通SQL查询(也就是SQLServer,Oracle,Sybase等数据库的查询语句,建议使用基于TSQL-92) 核心对象:CreateSQLQuery IList list = sess ...
- js的作用是临时修改 表单Action提交的地址,因为 又有新的动作需要把表单参数提交到 新的servlet中,这点很重要
JavaScript可以临时修稿 form表单的提交地址
- 使用ORC识别图片的文字
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- Django的model查询操作 与 查询性能优化
Django的model查询操作 与 查询性能优化 1 如何 在做ORM查询时 查看SQl的执行情况 (1) 最底层的 django.db.connection 在 django shell 中使用 ...
- Android中自动跳转
先看效果图吧 --------> --------> Activity类 package com.xm; import java.io.File; import j ...
- 《Drools7.0.0.Final规则引擎教程》第4章 4.2 auto-focus
auto-focus 在agenda-group章节,我们知道想要让AgendaGroup下的规则被执行,需要在代码中显式的设置group获得焦点.而此属性可配合agenda-group使用,代替代码 ...
- 拦截器springmvc防止表单重复提交【2】
[参考博客:http://my.oschina.net/mushui/blog/143397] 原理:在新建页面中Session保存token随机码,当保存时验证,通过后删除,当再次点击保存时由于服务 ...
- 一种 jquery 检索方案
整理自:http://www.cnblogs.com/linjiqin/archive/2011/03/18/1988464.html <!DOCTYPE HTML PUBLIC "- ...
- 转载.怎样在Quartus II中转化HDL文件为bsf文件?
步骤1 新建或打开Quartus II工程,用QII自带文本编辑器打开HDL文件. 图1 用QII自带的文本编辑器打开HDL文件 步骤2 选择File>Create / Update>Cr ...