Advertisement
nasarouf

LIS (longest increasing subsequence)

Feb 21st, 2015
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.56 KB | None | 0 0
  1. //longest increasing subsequence
  2. //nasarouf@cs.ubc.ca
  3.  
  4. int LISlast[MAX+1];
  5. //increasing
  6. int LIS(int *x, int n){
  7.     int l=0,*p;
  8.     LISlast[0]=-1;
  9.     for (int i=0; i<n; i++){
  10.         p=lower_bound(LISlast,LISlast+l+1,x[i]);
  11.         if (p==LISlast+l+1) ++l;  //get rid of the +1 ???
  12.         *p=x[i];
  13.     }
  14.     return l;
  15. }
  16. //non-decreasing
  17. int LNDS(int *x, int n){
  18.     int l=0,*p;
  19.     LISlast[0]=-1;
  20.     for (int i=0; i<n; i++){
  21.         p=upper_bound(LISlast,LISlast+l,x[i]);
  22.         if (p==LISlast+l) ++l;
  23.         *p=x[i];
  24.     }
  25.     return l;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement