Skip to content

Instantly share code, notes, and snippets.

@jhtan
Created February 20, 2014 18:17
Show Gist options
  • Save jhtan/9119947 to your computer and use it in GitHub Desktop.
Save jhtan/9119947 to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
vector<ll> LIS(vector<ll> X){
ll n = X.size(), L = 0, M[n+1], P[n];
ll lo,hi,mi;
L = 0;
M[0] = 0;
for(int i=0,j;i<n;i++){
lo = 0; hi = L;
while(lo!=hi){
mi = (lo+hi+1)/2;
if(X[M[mi]]<X[i]) lo = mi;
else hi = mi-1;
}
j = lo;
P[i] = M[j];
if(j==L || X[i]<X[M[j+1]]){
M[j+1] = i;
L = max(L,(ll)j+1);
}
}
ll a[L];
for(int i=L-1,j=M[L];i>=0;i--){
a[i] = X[j];
j = P[j];
}
return vector<ll>(a,a+L);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment