-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlis_count.cpp
More file actions
35 lines (31 loc) · 784 Bytes
/
lis_count.cpp
File metadata and controls
35 lines (31 loc) · 784 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
34
35
#include<bits/stdc++.h>
using namespace std;
int CeilIndex(std::vector<int>& v, int l, int r, int key) {
while (r - l > 1) {
int m = l + (r - l) / 2;
if (v[m] >= key) r = m;
else l = m;
}
return r;
}
int LongestIncreasingSubsequenceLength(std::vector<int>& v) {
if (!v.size())
return 0;
std::vector<int> tail(v.size(), 0);
int length = 1;
tail[0] = v[0];
for (size_t i = 1; i < v.size(); i++) {
if (v[i] < tail[0]) tail[0] = v[i];
else if (v[i] > tail[length - 1]) tail[length++] = v[i];
else tail[CeilIndex(tail, -1, length - 1, v[i])] = v[i];
}
return length;
}
int main() {
int n; scanf("%d",&n);
vector<int> v(n);
for (int i=0; i<n; i++)
scanf("%d",&v[i]);
printf("%d\n",LongestIncreasingSubsequenceLength(v));
return 0;
}