-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathcode(Increasing Subsequence).cpp
More file actions
53 lines (35 loc) · 995 Bytes
/
code(Increasing Subsequence).cpp
File metadata and controls
53 lines (35 loc) · 995 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int LongestIncreasingSubsequenceLength(vector<ll int>& v)
{
if (v.size() == 0)
return 0;
vector<ll int> tail(v.size(), 0);
ll int length = 1;
tail[0] = v[0];
for (ll int i = 1; i < v.size(); i++)
{
// binary search
auto b = tail.begin(), e = tail.begin() + length;
auto it = lower_bound(b, e, v[i]);
if (it == tail.begin() + length)
tail[length++] = v[i];
else
*it = v[i];
}
return length;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll int n;
cin>>n;
vector<ll int> v(n);
for(ll int&i : v)
cin>>i;
cout << LongestIncreasingSubsequenceLength(v) <<endl;
return 0;
}