最长上升/不下降子序列
2012-08-27 11:01
211 查看
最长上升/不下降子序列:(LIS)
有两种方法:
1.动态规划,O(n^2)
容易得出O(n^2)的DP递推公式:D[i]=max{D[j]}+1;(1<=j<i且E[j]<E[i])
D[i]为以元素i结尾的最长子序列个数。
这样经过两重循环一次遍历可以得到最长上升子序列。
代码:
View Code
有两种方法:
1.动态规划,O(n^2)
容易得出O(n^2)的DP递推公式:D[i]=max{D[j]}+1;(1<=j<i且E[j]<E[i])
D[i]为以元素i结尾的最长子序列个数。
这样经过两重循环一次遍历可以得到最长上升子序列。
代码:
View Code
#include <iostream> #include <stdio.h> #include <cstring> using namespace std; const int maxnum=100; int a[maxnum]; int stack[maxnum]; int top=-1; void function(int cur) { int l=0,r=top; while(l<=r) { int m=(l+r)>>1; if(stack[m]>=cur) //递增,stack[m]>cur为非递减 r=m-1; else l=m+1; } if(l>top) top=l; stack[l]=cur; } int main() { int n,i; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&a[i]); function(a[i]); } printf("%d\n",top+1); return 0; } /* 6 3 4 5 2 4 2 */
相关文章推荐
- DP---最长上升/下降子序列
- BZOJ1046(HAOI2007)[上升序列]--最长下降子序列预处理
- 动态规划——求最长下降/上升子序列
- noip1999 拦截导弹 (单调队列求解:最长下降子序列+最长上升子序列)
- 拦截导弹 最长上升/下降子序列
- DP---最长上升/下降子序列
- 动态规划——求最长下降/上升子序列
- poj 1836 Alignment -dp(合唱队形变式)-最长上升子序列+最长下降子序列
- 合唱队形 (dp-最长不上升/不下降子序列)
- POJ 1836 Alignment(DP max(最长上升子序列 + 最长下降子序列))
- 1065: 合唱队形(最长上升,下降子序列)
- 动态规划——求最长下降/上升子序列
- HDU 6197 array array array(最长上升或下降子序列)
- UVa 10534 - Wavio Sequence (简单DP 最长上升下降子序列)
- 最长不下降子序列(可以改成最长上升子序列)
- tyvj 1208 最长不下降子序列2 求序列b1,b2,b3,…,bm中所有长度(n)最大上升子序列的个数
- 蓝桥杯算法训练拦截导弹【最长上升子序列 & 最长非下降子序列nlogn 和 n^2】
- 动态规划——求最长下降/上升子序列
- POJ 1836 Alignment (最长上升 下降子序列)
- 动态规划——求最长下降/上升子序列