Longest Increasing Subsequence[LIS 最长上升子序列问题] (Longest Ordered Subsequence) POJ
来源:互联网 发布:重庆交通干部网络学校 编辑:程序博客网 时间:2024/05/18 03:42
Longest Ordered Subsequence
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 54481 Accepted: 24395
Description
A numeric sequence of ai is ordered ifa1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1,a2, ..., aN) be any sequence (ai1,ai2, ..., 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.
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
71 7 3 5 9 4 8
Sample Output
4
Source
Northeastern Europe 2002, Far-Eastern Subregion
动态规划,求最长上升子序列长度。
1.O(n2)的算法很好理解
#include <iostream>#include <cstdio>#include <cstring>#define inf 0x3f3f3f#define ms(x) memset(x,0,sizeof(x))using namespace std;int dp[10012];int a[10012];int main(){ int n,m; while(cin>>n) { ms(dp); ms(a); for(int i=0; i<n; i++) { scanf("%d",&a[i]); dp[i]=1; } int ans = 1; for(int i=1; i<n; i++) { for(int j=0; j<i; j++) { if(a[i]>a[j]) dp[i] = max(dp[i],dp[j]+1); ans= max(ans,dp[i]); } } cout<<ans<<endl; } return 0;}
2.队列优化,思路是模拟个栈,从前向后扫,将当前最优的上升子序列存进去。
长度最长为最优, 长度相同时末尾数最小即为最优。
附个例子
以下方块内容摘抄自:
http://blog.csdn.net/lianai911/article/details/20554865
假设存在一个序列d[1..9] = 2 1 5 3 6 4 8 9 7,可以看出来它的LIS长度为5。n下面一步一步试着找出它。我们定义一个序列B,然后令 i = 1 to 9 逐个考察这个序列。此外,我们用一个变量Len来记录现在最长算到多少了首先,把d[1]有序地放到B里,令B[1] = 2,就是说当只有1一个数字2的时候,长度为1的LIS的最小末尾是2。这时Len=1然后,把d[2]有序地放到B里,令B[1] = 1,就是说长度为1的LIS的最小末尾是1,d[1]=2已经没用了,很容易理解吧。这时Len=1接着,d[3] = 5,d[3]>B[1],所以令B[1+1]=B[2]=d[3]=5,就是说长度为2的LIS的最小末尾是5,很容易理解吧。这时候B[1..2] = 1, 5,Len=2再来,d[4] = 3,它正好加在1,5之间,放在1的位置显然不合适,因为1小于3,长度为1的LIS最小末尾应该是1,这样很容易推知,长度为2的LIS最小末尾是3,于是可以把5淘汰掉,这时候B[1..2] = 1, 3,Len = 2继续,d[5] = 6,它在3后面,因为B[2] = 3, 而6在3后面,于是很容易可以推知B[3] = 6, 这时B[1..3] = 1, 3, 6,还是很容易理解吧? Len = 3 了噢。第6个, d[6] = 4,你看它在3和6之间,于是我们就可以把6替换掉,得到B[3] = 4。B[1..3] = 1, 3, 4, Len继续等于3第7个, d[7] = 8,它很大,比4大,嗯。于是B[4] = 8。Len变成4了第8个, d[8] = 9,得到B[5] = 9,嗯。Len继续增大,到5了。最后一个, d[9] = 7,它在B[3] = 4和B[4] = 8之间,所以我们知道,最新的B[4] =7,B[1..5] = 1, 3, 4, 7, 9,Len = 5。于是我们知道了LIS的长度为5。!!!!! 注意。这个1,3,4,7,9不是LIS,它只是存储的对应长度LIS的最小末尾。有了这个末尾,我们就可以一个一个地插入数据。虽然最后一个d[9] = 7更新进去对于这组数据没有什么意义,但是如果后面再出现两个数字 8 和 9,那么就可以把8更新到d[5], 9更新到d[6],得出LIS的长度为6。然后应该发现一件事情了:在B中插入数据是有序的,而且是进行替换而不需要挪动——也就是说,我们可以使用二分查找,将每一个数字的插入时间优化到O(logN)~~~~~于是算法的时间复杂度就降低到了O(NlogN)~!
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <algorithm>using namespace std;const int N =10100;int s[N];int lis(int a[], int n){ int mid, top, low, high; top = 0; s[0] = -1; for(int i=0; i<n; i++) { if(a[i] > s[top]) s[++top] = a[i]; else { low = 1; high = top; while(low <= high) { mid = (low + high)/2; if(a[i]>s[mid]) low = mid + 1; else high = mid - 1; } s[low] = a[i]; } } return top;}int main(){ int n; int a[N]; while(cin>>n) { for(int i=0; i<n; i++) scanf("%d",&a[i]); int ans = lis(a, n); cout<<ans<<endl; } return 0;}
3.排序后找两串最长公共子串
阅读全文
0 0
- Longest Increasing Subsequence[LIS 最长上升子序列问题] (Longest Ordered Subsequence) POJ
- poj 2533 Longest Ordered Subsequence(LIS(最长上升子序列))
- POJ 2533 Longest Ordered Subsequence(LIS:最长上升子序列)
- 最长上升子序列LIS(Longest Increasing Subsequence)
- 最长上升子序列 LIS(Longest Increasing Subsequence)(转)
- 最长上升子序列LIS(Longest Increasing Subsequence)
- 最长上升子序列(Longest Increasing Subsequence,LIS)
- poj 2533 Longest Ordered Subsequence(最长上升子序列)
- POJ 2533 Longest Ordered Subsequence(最长上升子序列)
- POJ 2533 Longest Ordered Subsequence(最长上升子序列)
- POJ 2533Longest Ordered Subsequence(最长上升子序列)
- 【POJ 2533】Longest Ordered Subsequence(最长上升子序列LIS)
- 【POJ2533】Longest Ordered Subsequence(LIS-最长上升子序列/DP)
- 最长上升子序列 LIS(Longest Increasing Subsequence)
- 最长上升子序列 LIS(Longest Increasing Subsequence)---DP
- 最长上升子序列 LIS(Longest Increasing Subsequence)
- 最长上升子序列 LIS(Longest Increasing Subsequence)
- 最长上升子序列 LIS (Longest Increasing Subsequence)
- 一个类的六个默认函数
- pascal转c++之路(一)
- x264中的汇编:x86inc.asm——001
- 通过字节流复制粘贴图片
- java web 分页技术
- Longest Increasing Subsequence[LIS 最长上升子序列问题] (Longest Ordered Subsequence) POJ
- js打印div指定区域内容并保留css样式
- Qt编写的软件在线升级系统(服务器端)
- 控制台console常用的一些命令
- X264中的汇编:sad-a.asm——002
- zcmu-1957: 乌鸦坐飞机
- Java 并发 ---AbstractQueuedSynchronizer(同步器)-独占模式
- Win7 Ubuntu双系统安装踩坑记录
- C++中虚函数工作原理和(虚)继承类的内存占用大小计算