-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLIS.cpp
More file actions
51 lines (46 loc) · 1.09 KB
/
LIS.cpp
File metadata and controls
51 lines (46 loc) · 1.09 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <bits/stdc++.h>
using namespace std;
//Non decreasing, can be easily modified to any
//However, this implementation relies on value indexation, and as such
//if values can be too large, compression is needed.
#define N 1000001
int n = N-1;
int t[N];
void upd(int pos, int v){
for(;pos <= n; pos += pos & -pos){
if(v > t[pos]){
t[pos] = v;
}
}
}
int qu(int pos){
int ret = 0;
for(;pos > 0; pos -= pos & -pos){
if(t[pos] > ret){
ret = t[pos];
}
}
return ret;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n;
cin >> n;
int in[n];
for(auto &i: in) cin >> i;
vector<int> vec(in,in+n);
sort(vec.begin(),vec.end());
vec.erase(unique(vec.begin(),vec.end()),vec.end());
for(auto &i: in){
i = lower_bound(vec.begin(),vec.end(),i)-vec.begin()+1;
}
int ans = 0;
for(int i = 0; i < n; ++i){
int nv = qu(in[i])+1; //there must be no zeroes (fenwick)
if(nv > ans){
ans = nv;
}
upd(in[i],nv);
}
cout << ans << '\n';
}