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),是指两个 ...
随机推荐
- ofbiz研究
近段时间,刚有有时间研究了下ofbiz ; 目前还是刚开始,后期会记录过程 有一起研究的没
- cookie操作和代理
cookie操作 爬取豆瓣个人主页 # -*- coding: utf-8 -*- import scrapy class DoubanSpider(scrapy.Spider): name = 'd ...
- DNS无法区域传送(axfr,ixfr)
这两天博主在学习dns服务器的配 首先简单介绍一下axfr,ixfr axfr:完全区域传送 ixfr :增量区域传送 主要是在dns主从服务器上面进行备份更新的. ----------------- ...
- Fruits【水果】
Fruits Many of us love July because it's the month when nature's berries and stone fruits are in abu ...
- realloc函数的用法
realloc(void *__ptr, size_t __size):更改已经配置的内存空间,即更改由malloc()函数分配的内存空间的大小. 如果将分配的内存减少,realloc仅仅是改变索引的 ...
- Altium Designer 快捷键使用整理
Altium Designer 快捷键 一.原理图部分 1.原理图元件自动编号 原理图中快捷键 T+A 2.原理图与PCB交互设计查找 原理图中选中一个元件跳转到PCB中相应的位置T+S 3.原理图中 ...
- Eclipse 创建 Java 接口---Eclipse教程第11课
打开新建 Java 接口向导 新建 Java 接口向导可以创建新的 Java 接口.打开向导的方式有: 点击 File 菜单并选择 New > Interface 在 Package Explo ...
- imageX.exe
imageX 编辑ImageX 是一个命令行工具,原始设备制造商 (OEM) 和公司可以使用它来捕获.修改和应用基于文件的磁盘映像以进行快速部署.ImageX 可以使用 Windows 映像 (.wi ...
- 怎么设置才能让外网ip可以访问mysql数据库[转]
转自: http://www.hongyanliren.com/89.html 使用mysql中,很多人都会遇到这样的问题:在vps服务器或者云服务器上安装了mysql后,使用其他工具在外网ip之下根 ...
- copyEvens
public int[] copyEvens(int[] nums, int count) { int newIndex=0; int i=0; int newArray[] = new int[co ...