二分求最长上升子序列
2017-02-09 20:52
274 查看
简述
我们知道N2求最长上升子序列的方法,即f[i]=max(f[i],f[j]+1)(j=1 i−1,f[i]>f[j])
然而,我们可以用二分求最长上升子序列。
由于我们知道子序列c,c[1]<c[2]<… <c
。
所以我们二分出一个位置t,将c[t]的值更新为a[i]。
如果t>ans,证明最长上升子序列的长度可以+1,于是ans++。
代码
#include<iostream> #include<cstdio> #include<cmath> #define N 100010 #define fo(i,a,b) for(i=a;i<=b;i++) using namespace std; int i,j,l,n,m,t,s,ans; int a ,c ; char ch; int ef(int l,int r,int x) { if (l==r) return l; int mid=(l+r)/2; if (x>c[mid]) return ef(mid+1,r,x);else return ef(l,mid,x); } int main() { scanf("%d",&n); fo(i,1,n) scanf("%d",&a[i]); c[0]=-2147483647; ans=0; fo(i,1,n) { j=ef(1,ans+1,a[i]); c[j]=a[i]; if (j==ans+1) ans++; } printf("%d",ans); }
相关文章推荐
- HDU 1025 最长上升子序列变形 (dp+二分)
- bzoj 3173: [Tjoi2013]最长上升子序列(离线二分+树状数组)
- ZOJ2283 Challenge of Wisdom(DP+二分求最长非上升子序列)
- UVA 10534 Wavio Sequence(二分 + 最长上升子序列)
- hdu 1025 二分查找+最长上升子序列
- 最长上升子序列——O (nlogn)算法原因解析!为什么这样可以求出来!(附带动态规划dp + 二分查找讲解)
- hdu 5256 序列变换(最长上升子序列&&二分)
- 【最长上升子序列+二分优化】麻烦的聚餐 egroup.pas/c/cpp
- LIS(最长上升子序列)的 DP 与 (贪心+二分) 两种解法
- 使用二分查找求解最长上升子序列
- 【基础练习】【二分】codevs2188 最长上升子序列(限定元素)题解
- POJ2533 -Longest Ordered Subsequence最长上升子序列-动规和nlogn二分算法详解
- 【最长上升子序列+二分优化】麻烦的聚餐 egroup.pas/c/cpp
- POJ 1631-Bridging signals(最长上升子序列+二分搜索)
- 杭电1950 Bridging signals(二分求最长上升子序列)
- NYOJ 214 最长上升子序列加强版(二分+dp)
- 最长上升子序列变形题-二分查找DP
- hdu6197array array array(最长上升子序列二分版)
- HDU-1905-Bridging signals(二分求最长上升子序列)
- 玲珑 1097(不相交的二分匹配->最长上升子序列)@