Longest Ordered Subsequence
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 66763   Accepted: 29906

Description

A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1a2, ..., aN) be any sequence (ai1ai2, ..., aiK), where 1 <= i1 < i2 < ... < iK <= N. For example, sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, e. g., (1, 7), (3, 4, 8) and many others. All longest ordered subsequences are of length 4, e. g., (1, 3, 5, 8).

Your program, when given the numeric sequence, must find the length of its longest ordered subsequence.

Input

The first line of input file contains the length of sequence N. The second line contains the elements of sequence - N integers in the range from 0 to 10000 each, separated by spaces. 1 <= N <= 1000

Output

Output file must contain a single integer - the length of the longest ordered subsequence of the given sequence.

Sample Input

7
1 7 3 5 9 4 8

Sample Output

4

最长递增子序列问题,水题。理解好状态转移式怎么得到就行了。
状态转移式是d(i) = max(dp[j] + 1,dp[i])(a[i] > a[j])(j < i) (如果为最长不下降子序列的话a[i] >= a[j]就行,并且j < i);其中dp[i]要先为1,因为无论如何这个子序列的长度的最小情况为1。
打表就行
对了,最后得到的dp[i]中i从1到n时,各自的dp[i]代表前i个数的最长递增子序列的最长长度。不一定dp[n]是最大的,要先比较,从中选出最大的。 C++ 代码:
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
const int maxn = ;
int a[maxn],dp[maxn];
int main(){
int n;
memset(a,,sizeof(a));
scanf("%d",&n);
for(int i = ; i < n; i++){
cin>>a[i];
}
memset(dp,,sizeof(dp));
for(int i = ; i < n; i++){
dp[i] = ;
for(int j = ; j < i; j++){
if(a[i] > a[j] && dp[j] + > dp[i])
dp[i] = dp[j] + ;
}
}
int ans = -;
for(int i = ; i < n; i++){
ans = max(ans,dp[i]);
}
printf("%d\n",ans);
return ;
}

(线性DP LIS)POJ2533 Longest Ordered Subsequence的更多相关文章

  1. POJ2533 Longest Ordered Subsequence —— DP 最长上升子序列(LIS)

    题目链接:http://poj.org/problem?id=2533 Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 6 ...

  2. POJ2533——Longest Ordered Subsequence(简单的DP)

    Longest Ordered Subsequence DescriptionA numeric sequence of ai is ordered if a1 < a2 < ... &l ...

  3. [POJ2533]Longest Ordered Subsequence<dp>

    题目链接:http://poj.org/problem?id=2533 描述: A numeric sequence of ai is ordered if a1 < a2 < ... & ...

  4. POJ2533 Longest Ordered Subsequence 【最长递增子序列】

    Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 32192   Acc ...

  5. poj-2533 longest ordered subsequence(动态规划)

    Time limit2000 ms Memory limit65536 kB A numeric sequence of ai is ordered if a1 < a2 < ... &l ...

  6. POJ2533 Longest ordered subsequence

    Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 41984   Acc ...

  7. POJ2533 Longest Ordered Subsequence (线性DP)

    设dp[i]表示以i结尾的最长上升子序列的长度. dp[i]=max(dp[i],dp[j]+1). 1 #include <map> 2 #include <set> 3 # ...

  8. POJ-2533.Longest Ordered Subsequence (LIS模版题)

    本题大意:和LIS一样 本题思路:用dp[ i ]保存前 i 个数中的最长递增序列的长度,则可以得出状态转移方程dp[ i ] = max(dp[ j ] + 1)(j < i) 参考代码: # ...

  9. POJ2533:Longest Ordered Subsequence(LIS)

    Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence ...

随机推荐

  1. Socket初见

    前端代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <t ...

  2. npm火速上手

    npm,即node  package manager,翻译过来就是“node包管理工具”.“node包”是啥呢?它就是jquery啦.bootstrap啦之类的各种版本. 1.npm的安装 第一步,下 ...

  3. linux读取Windows的txt文件问题

    问题:Windows下生成的txt文件,在Linux下读取时会读取到多余字符(如: ^M) 原因:Windows和Linux下的换行符不一致 解决:在Linux代码中将多余字符去掉 ) buf = b ...

  4. 给dom对象添加事件

  5. 洛谷 P1538 迎春舞会之数字舞蹈

    题目背景 HNSDFZ的同学们为了庆祝春节,准备排练一场舞会. 题目描述 在越来越讲究合作的时代,人们注意的更多的不是个人物的舞姿,而是集体的排列. 为了配合每年的倒计时,同学们决定排出——“数字舞蹈 ...

  6. Element UI 中组件this.$message报错

    最近在做毕设的时候,用Element UI中的消息提示message一直报以下的错误: 展示的效果也不好看,没有图标什么的: 但我明明有在main.js引入了element-ui 呀,因为毕设时间很赶 ...

  7. Spring IOC注入接口多实现解决

    前期面试的时候被面试官问到,Spring注入某接口,而接口有多实现,应该如何处理.接口多实现很常见,但在业务逻辑开发中,需要考虑注入某接口的多个实现问题的情况并不多见.当时是一脸懵逼,目前有时间,就做 ...

  8. Tomcat服务的安装与配置

    介绍 百度百科 Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache.Sun 和其他一些公司及个人共同开 ...

  9. POJ2187-Beauty Contest-凸包

    平面最远点对 由于点数为1e5,而整数点的情况下,凸包上点的个数为sqrt(M),M为范围. 这样求出凸包之后n^2枚举维护距离就可以了 否则就用旋转卡壳. 这里用了挑战上的做法,比较简洁. #inc ...

  10. 【BZOJ2144】Throw 数论

    题目大意 给你三个数\(a,b,c\),每次你可以选择一个数\(s_1\),再选择一个数\(s_2\),把\(s_1\)变成\(2s_2-s_1\),但要求\(s_3\)不在\(s_1\)到\(2s_ ...