最长非降 nlogn 带路径标记
来源:互联网 发布:班班通教学软件 源码 编辑:程序博客网 时间:2024/09/21 09:26
http://en.wikipedia.org/wiki/Longest_increasing_subsequence 本文来自wiki
X[i]就是表示原始序列
M[j] 存的是长度为j 的子序列,最后一个数的位置在M[j]
P[k] 表示第k个元素的决策前驱是p[k]
X[M[1]] X[M[2]] X[M[L]] 是一个单调队列,这个实现有点巧,用M数组来记录单调队列元素的下标。
P = array of length N M = array of length N + 1 L = 0 for i in range 0 to N-1: // Binary search for the largest positive j ≤ L // such that X[M[j]] < X[i] lo = 1 hi = L while lo ≤ hi: mid = (lo+hi)/2 if X[M[mid]] < X[i]: lo = mid+1 else: hi = mid-1 // After searching, lo is 1 greater than the // length of the longest prefix of X[i] newL = lo // The predecessor of X[i] is the last index of // the subsequence of length newL-1 P[i] = M[newL-1] if newL > L: // If we found a subsequence longer than any we've // found yet, update M and L M[newL] = i L = newL else if X[i] < X[M[newL]]: // If we found a smaller last value for the // subsequence of length newL, only update M M[newL] = i // Reconstruct the longest increasing subsequence S = array of length L k = M[L] for i in range L-1 to 0: S[i] = X[k] k = P[k] return S
Because the algorithm performs a single binary search per sequence element, its total time can be expressed using Big O notation as O(n log n). Fredman (1975) discusses a variant of this algorithm, which he credits to Donald Knuth; in the variant that he studies, the algorithm tests whether each value X[i] can be used to extend the current longest increasing sequence, in constant time, prior to doing the binary search. With this modification, the algorithm uses at mostn log2 n − n log2log2 n + O(n) comparisons in the worst case, which is optimal for a comparison-based algorithm up to the constant factor in the O(n) term.[5]
Length bounds[edit]
According to the Erdős–Szekeres theorem, any sequence of n2+1 distinct integers has either an increasing or a decreasing subsequence of length n + 1.[6][7] For inputs in which each permutation of the input is equally likely, the expected length of the longest increasing subsequence is approximately 2√n. [8] In the limit as n approaches infinity, the length of the longest increasing subsequence of a randomly permuted sequence of n items has a distribution approaching theTracy–Widom distribution, the distribution of the largest eigenvalue of a random matrix in the Gaussian unitary ensemble.[9]
PS:
Szekeres theorem,
For given r, s they showed that any sequence of length at least (r − 1)(s − 1) + 1 contains a monotonically increasing subsequence of length r or a monotonically decreasing subsequence of length s.
- 最长非降 nlogn 带路径标记
- (LIS)最长递增/递减子序列(带路径)模板 O(NlogN)
- HDOJ 4604 - Deque O(nlogn)的最长非升(非降)
- hdu 5532【最长非递增子序列 时间复杂度 nlogn】
- LIS的另一种简单做法nlogn(带路径)
- 最长非上升/非下降子序列(O(nlogn)非连续)
- 带标记非递归的动态树
- 最长公共子序列及最长递增子序列NlogN算法及路径记录
- nlogn最长单调递增
- nlogn最长上升&最长下降
- 简单dp hdu 5532 Almost Sorted Array nlogn 的最长非严格单调子序列
- HDU 1160(FatMouse's Speed)最长不降子序列nlogn+路径记录
- CDOJ(UESTC) 251 导弹拦截(最长递增子序列nlogn算法+路径打印)
- 计蒜客 求数组的最长递减子序列(nlogn+路径打印)
- 蓝桥杯 算法训练 拦截导弹【最长上升子序列 & 最长非下降子序列 nlogn 和 n^2】
- 最长公共子序列(nlogn)
- 最长上升子序列 nlogn
- 最长非降子序列O(nlogn)
- 关于iOS自动布局
- java import的机制
- 2015校招 面试(软件研发)
- 动态生成radiobutton
- Android的SurfaceView使用
- 最长非降 nlogn 带路径标记
- 2014.09.22 Jfinal注解方式自动配置路由和数据库映射
- 【Axure教程】Axure中使用表格的技巧
- C系列函数原型之-StrStr(char *str,char *subStr)的实现
- C语言_堆和栈
- VisualStudioVS2010统计代码行数
- windows 编译ffmpeg 播放 rtmp
- 关于数字证书和数字签名的一些认识
- Python安装pandas,matplotlib