DP———3.最长上升子序列的和
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
求最长上升子序列
状态转移方程为 dp[i]=max(dp[i],dp[j]+a[i]);
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1e6+;
int n,ans,tem;
int a[maxn];
int dp[maxn];
int main(){
while(scanf("%d",&n) &&n){
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
memset(dp,,sizeof(dp));
dp[]=a[];
for(int i=;i<=n;i++){
dp[i]=a[i];
for(int j=;j<i;j++){
if(a[j]<a[i]){
dp[i]=max(dp[i],dp[j]+a[i]);
}
}
}
ans=dp[];
for(int i=;i<=n;i++){
ans=max(ans,dp[i]);
}
printf("%d\n",ans);
}
return ;
}
DP———3.最长上升子序列的和的更多相关文章
- hdu1025 dp(最长上升子序列LIS)
题意:有一些穷国和一些富国分别排在两条直线上,每个穷国和一个富国之间可以建道路,但是路不能交叉,给出每个穷国和富国的联系,求最多能建多少条路 我一开始在想有点像二分图匹配orz,很快就发现,当我把穷国 ...
- [NYIST16]矩形嵌套(DP,最长上升子序列)
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=16 像套娃一样把矩形套起来.先给矩形从小到大排序,然后做最长上升子序列就行 /* ━━━━ ...
- poj2533--Longest Ordered Subsequence(dp:最长上升子序列)
Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 33943 Acc ...
- POJ-1458.CommonSubsequence.(DP:最长公共子序列裸题)
本题大意:给出两个字符串,让你求出最长公共子序列的长度并输出. 本题思路:本题是经典的DP问题,由于是两个字符串,那么我们就用一个二维数组来进行区分,用dp[ i ][ j ]来表示在s1和s2中分别 ...
- hdu1257 dp(最长上升子序列)
题意:有一种拦截系统,可以打击导弹,但是打击的高度会逐渐下降,因此为了防御导弹攻击,就必须用多个系统,现给出一列导弹依次的高度,求最少需要的系统数. 这道题是最长上升子序列问题,但是我一开始其实并没有 ...
- 算法练习--- DP 求解最长上升子序列(LIS)
问题描写叙述: 对于2,5,3,1,9,4,6,8,7,找出最长上升子序列的个数 最长上升子序列定义: 对于i<j i,j∈a[0...n] 满足a[i]<a[j] 1. 找出DP公式:d ...
- dp之最长递增子序列模板poj3903
最长递增子序列,Longest Increasing Subsequence 下面我们简记为 LIS.排序+LCS算法 以及 DP算法就忽略了,这两个太容易理解了. 假设存在一个序列d[1..9] = ...
- [DP题]最长上升子序列
最长上升子序列 总时间限制:2000ms 内存限制:65536kB 描述 一个数的序列bi,当b1 < b2 < ... < bS的时候,我们称这个序列是上升的.对于给定的一个序列( ...
- 动态规划(DP),最长递增子序列(LIS)
题目链接:http://poj.org/problem?id=2533 解题报告: 状态转移方程: dp[i]表示以a[i]为结尾的LIS长度 状态转移方程: dp[0]=1; dp[i]=max(d ...
- 51nod 1183 编辑距离【线性dp+类似最长公共子序列】
1183 编辑距离 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 编辑距离,又称Levenshtein距离(也叫做Edit Distance),是指两个 ...
随机推荐
- Servlet学习笔记07——什么是cookie,session?
7)cookie的路径问题 a.什么是cookie路径问题? 浏览器在向服务器上的某个地址发送请求时, 会查看cookie的路径是否与该地址匹配,只有 匹配的cookie才会被发送. b.cookie ...
- NuGet管理和还原程序包
在很多开源的程序下载下来不能使用,一般都是平台X86 和X64没有修改,还一个就是程序缺少资源包文件.用Nuget还原即可: 一般建议先修改好平台,然后用NuGet还原程序包.
- 基于WSAAsyncSelect模型的两台计算机之间的通信
任务目标 编写Win32程序模拟实现基于WSAAsyncSelect模型的两台计算机之间的通信,要求编程实现服务器端与客户端之间双向数据传递.客户端向服务器端发送"请输出从1到1000内所有 ...
- java-访问控制修饰符
访问权限 public 任何情况都可以访问 默认包 本包范围内可以访问到 protect 同一个包里的所有类所可以访问:所有子类(子类可以不和父类在同一个包)都可以访问 privat ...
- mysql的一些相关的命令(2013-05-05-bd 写的日志迁移
cmd中连接:mysql -u用户名 -p用户密码 (注:u与root可以不用加空格,其它也一样)断开:exit (回车) --建立数据库creata database 数据库名;--切换到数据库下工 ...
- 词向量1.md
词向量 我们以句子分类为例,我们使用深度学习模型对句子进行分类,本质上这个模型的接受的舒服需要是数值型.因为文字是人们抽象出来的一个概念,这个 东西是不能被计算机直接理解的,我们需要人为的将这个文字转 ...
- 493. Reverse Pairs
// see more at https://www.youtube.com/watch?v=j68OXAMlTM4 // https://leetcode.com/problems/reverse- ...
- Reward HDU - 2647
传送门 Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to dis ...
- android 布局preview 技巧
最近开始看老外写的文章,博客,嗯,不能说比国人写的好,但是感觉看着很爽.真的,一手资料就是爽. 嗯,自己做得不错,第一次看外文博客,我知道自己怎么看的,一句话一句话看下来的,越看越有感觉. 下面这个 ...
- (1)strchr
const char * strchr ( const char * str, int character ); char * strchr ( char * str, int character ) ...