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. SSL 握手过程

    SSL协议的握手过程 SSL 协议既用到了公钥加密技术又用到了对称加密技术,对称加密技术虽然比公钥加密技术的速度快,可是公钥加密技术提供了更好的身份认证技术.SSL 的握手协议非常有效的让客户和服务器 ...

  2. VisionTimer BUG && Start

    void Start() { vp_Timer.In(0.0f, delegate() { Debug.Log("Start"); }, 10, 1.0f); } Version ...

  3. Kruskal最小生成树

    并查集+kruskal==>MST 效率很低 #include <iostream> using namespace std; #define MAX 105 //自己设置最大值 / ...

  4. unity3d GameObject.Find 严格区分大小写的

    GameObject.Find 查找 static function Find (name : string) : GameObject Description描述 Finds a game obje ...

  5. WPF中log4net的用法

    WPF中如何使用log4nethttp://www.cnblogs.com/C-Sharp2/archive/2013/04/12/WPF-LOG4NET.html Apache log4net Ma ...

  6. 独立IP与共享IP的区别

    做网站选择独立IP还是共享IP,相信很多站长都在此纠结过,自己不使用服务器的时候从来没有关心过独立IP和共享IP的究竟有什么具体的差别.但当自己真正用到的时候,才发现:同样都是 IP,差别不是一般的大 ...

  7. 重写equals()方法时,需要同时重写hashCode()方法

    package com.wangzhu.map; import java.util.HashMap; /** * hashCode方法的主要作用是为了配合基于散列的集合一起正常运行,<br/&g ...

  8. 毕向东JAVA视频讲解(第六课)

    用java语言对现实生活中的事物进行描述. 通过类的形式来体现的. 怎么描述呢? 对于事物描述通常只关注两方面. 一个是属性,一个是行为. 只要明确该事物的属性和行为并定义在类中即可. 对象:其实就是 ...

  9. java登陆验证码与JS无刷新验证

    最近公司的项目的登陆模块由我负责,所以就做了个登陆小功能进行练手,其包括了用jQuery对用户名和密码进行不为null验证,和出于安全性考虑加了一个验证码的校验 别的不说先上代码 controller ...

  10. Android Bundle的使用

    发送数据: Bundle bundle = new Bundle(); bundle.putString("sex" , "男人"); bundle.putDo ...