Title:

http://poj.org/problem?id=2533

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
这个是经典的动态规划题,像我这种渣渣,连这个都要看好久,好吧,其实简单的DP还好,就是nlogn复杂度的,看了好久。嗯,但是呢,网上的那些代码都没有我写的简洁,好吧,这也是聊以自慰呢。。
思路(1): O(n^2)

令A[i]表示输入第i个元素,D[i]表示从A[1]到A[i]中以A[i]结尾的最长子序列长度。对于任意的0 <  j <= i-1,如果A(j) < A(i),则A(i)可以接在A(j)后面形成一个以A(i)结尾的新的最长上升子序列。对于所有的 0 <  j <= i-1,我们需要找出其中的最大值。

DP状态转移方程:

D[i] = max{1, D[j] + 1} (j = 1, 2, 3, ..., i-1 且 A[j] < A[i])

解释一下这个方程,i, j在范围内:

如果 A[j] < A[i] ,则D[i] = D[j] + 1

如果 A[j] >= A[i] ,则D[i] = 1

int main(){
int a[SIZE];
int d[SIZE];
int n;
cin>>n;
for (int i = ; i < n; i++)
cin>>a[i];
int m = INT_MIN;
for (int i = ;i < n; i++){
d[i] = ;
for (int j = ; j < i; j++){
if (a[j] < a[i])
d[i] = max(d[i],d[j]+);
}
m = max(m,d[i]);
}
cout<<m<<endl;
//system("pause");
}

(2)nlogn的解法。

重新定义下dp[i]

dp[i] 的意思是所有长度为i+1的LIS中末尾元素的最小值

那么最开始,dp[0] = a[0]

因为dp是一个有序数组,所以每次我们都去这个数组中寻找a[i]的位置,例如dp = {2,4,6},如果a[i] = 5,那么a[i]的位置应该是4~6之间,所以返回index = 2.怎么理解呢,dp[i]的定义!所以a[i] = 5,那么有两个长度的最小值都比a[i]小,那加入a[i],这个长度肯定就是3了,然后这个index=2,同时,比较5和6,我们要选择最小的值。我的搜索代码包含了边界的情况,因此,在主函数中就不需要判断。

int search(int * s, int t,int l, int r){
while (l <= r){
int m = (l + r)/;
if (s[m] == t){
return m;
}else if (s[m] < t){
l++;
}else{
r--;
}
}
return l;
}
int main(){
int a[SIZE];
int n;
cin>>n;
for (int i = ; i < n; i++){
cin>>a[i];
}
int stack[SIZE]; fill(stack,stack+SIZE,INT_MAX);
stack[] = a[];
int cur_index = ;
for (int i = ; i < n; i++){
int j = search(stack,a[i],,cur_index);
stack[j] = min(stack[j],a[i]);
if (j == cur_index)
cur_index++;
}
cout<<cur_index;
//system("pause");
}
												

POJ:最长上升子序列的更多相关文章

  1. OpenJudge 2757 最长上升子序列 / Poj 2533 Longest Ordered Subsequence

    1.链接地址: http://poj.org/problem?id=2533 http://bailian.openjudge.cn/practice/2757 2.题目: 总Time Limit: ...

  2. poj 2533 Longest Ordered Subsequence 最长递增子序列

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098562.html 题目链接:poj 2533 Longest Ordered Subse ...

  3. POJ 1458 Common Subsequence(LCS最长公共子序列)

    POJ 1458 Common Subsequence(LCS最长公共子序列)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?c ...

  4. POJ 3903 Stock Exchange (E - LIS 最长上升子序列)

    POJ 3903    Stock Exchange  (E - LIS 最长上升子序列) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action ...

  5. POJ 2533 - Longest Ordered Subsequence - [最长递增子序列长度][LIS问题]

    题目链接:http://poj.org/problem?id=2533 Time Limit: 2000MS Memory Limit: 65536K Description A numeric se ...

  6. POJ 1159 Palindrome(最长公共子序列)

    Palindrome [题目链接]Palindrome [题目类型]最长公共子序列 &题解: 你做的操作只能是插入字符,但是你要使最后palindrome,插入了之后就相当于抵消了,所以就和在 ...

  7. POJ 1458 最长公共子序列(dp)

    POJ 1458 最长公共子序列 题目大意:给出两个字符串,求出这样的一 个最长的公共子序列的长度:子序列 中的每个字符都能在两个原串中找到, 而且每个字符的先后顺序和原串中的 先后顺序一致. Sam ...

  8. [poj 1533]最长上升子序列nlogn树状数组

    题目链接:http://poj.org/problem?id=2533 其实这个题的数据范围n^2都可以过,只是为了练习一下nlogn的写法. 最长上升子序列的nlogn写法有两种,一种是变形的dp, ...

  9. POJ 1458 Common Subsequence(最长公共子序列LCS)

    POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...

随机推荐

  1. hibernate4.0中SessionFactory的创建

    创建SessionFactory 首先创建Configuration对象,主要方式是: new Configuration().configure() 默认情况下Hibernate会去classPat ...

  2. soap 路由

    下面主要通过项目实例来具体阐述如何实现wse路由和一些项目开发中的细节.本人水平有限,有不对的地方,请朋友们不吝赐教. 在开始项目之前,先了解一下路由的概念,所谓"路由",是指把数 ...

  3. Unity3dBug - OnEnable

    最近 项目 因为 使用 active 代替 instantiate机制,很多时候 OnEnable 代理 OnStart. 然后发现一个 奇怪的 问题 void Awake() { Debug.Log ...

  4. Properties --- C++读配置信息的类

    http://blog.csdn.net/billow_zhang/article/details/4304980 在开发实践中,积累了一些通用的C++ 类库,在此写出来给大家分享.也希望能给出更好的 ...

  5. Chpater 10: Sorting

    Internal Sort: Bubble  O(n2) Selection O(n2) Insertion O(n2) Shell O(nlogn) Merge O(nlogn) Heap O(nl ...

  6. Struts2 常用的常量配置

    在struts2-core-2.1.8.1.jar的org.apache.struts2包下面的default.properties资源文件里可以查到常用的常量配置,这些不用刻意的记住:忘记的时候可以 ...

  7. hdu 4111 Alice and Bob 博弈论

    这里有2种方法: 方法一:求SG函数 sg[i][j]:i表示1的个数,j表示合并操作的步数. 这共有4种操作: 1.消除一个1: 2.减掉一个1: 3.合并2个1: 4.把1合并到另外不是1中. 代 ...

  8. Java 按字节获得字符串(中文)长度

    引自:http://songjianyong.iteye.com/blog/1552973 package cn.com.songjy.test; import java.io.Unsupported ...

  9. spring autoWire注解

    1.autowire注解,可以用来获得applicationContext,ResourceLoader,BeanFactory的注入 autoWire会获得相应资源 2.autoWire注解还可以用 ...

  10. Linux autoconf和automake使用

    作为Linux下的程序开发人员,一定都遇到过Makefile,用make命令来编译自己写的程序确实是很方便.一般情况下,大家都是手工写一个简单Makefile,如果要想写出一个符合自由软件惯例的Mak ...