-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLIS.cpp
More file actions
33 lines (32 loc) · 733 Bytes
/
LIS.cpp
File metadata and controls
33 lines (32 loc) · 733 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//complexity: O(nlgn)
/*
define limit first.
define LIS_SZ = 1 for every test case
define lastVal[] = infinity first
LB(s,e,val) finds the largest index i so that lastVal[i]<=val
genLIS( *a , n ) genarets LIS_SZ for array a[] of length 'n'
*/
#define lim 100010
LL arr[lim];
LL lastVal[lim];
LL LIS_SZ = 1;
LL LB( LL s, LL e , LL val ){
LL ret = 0 , mid ;
while( s<=e ){
mid = ((s+e)/2);
if( lastVal[mid]<=val ){
ret = mid;
s = mid + 1;
}
else e = mid - 1;
}
return ret;
}
void genLIS( LL *a , LL n ){
FOR(i,1,n) lastVal[i] = 1e18;
FOR(i,1,n){
LL upInd = LB( 1,LIS_SZ,a[i] ) + 1;
lastVal[upInd] = a[i];
LIS_SZ = max( LIS_SZ , upInd );
}
}