nasarouf

LIS (longest increasing subsequence)

Feb 21st, 2015
151
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.  
  3. int LISlast[MAX+1];
  4. //increasing
  5. int LIS(int *x, int n){
  6.     int l=0,*p;
  7.     LISlast[0]=-1;
  8.     for (int i=0; i<n; i++){
  9.         p=lower_bound(LISlast,LISlast+l+1,x[i]);
  10.         if (p==LISlast+l+1) ++l;  //get rid of the +1 ???
  11.         *p=x[i];
  12.     }
  13.     return l;
  14. }
  15. //non-decreasing
  16. int LNDS(int *x, int n){
  17.     int l=0,*p;
  18.     LISlast[0]=-1;
  19.     for (int i=0; i<n; i++){
  20.         p=upper_bound(LISlast,LISlast+l,x[i]);
  21.         if (p==LISlast+l) ++l;
  22.         *p=x[i];
  23.     }
  24.     return l;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment