最长非降 nlogn 带路径标记

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 rs 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.




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值