-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLIS.cpp
More file actions
50 lines (42 loc) · 1.29 KB
/
LIS.cpp
File metadata and controls
50 lines (42 loc) · 1.29 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;
// Binary search (note boundaries in the caller)
int CeilIndex(vector<int> &v, int l, int r, int key)
{ // search space is of the form 0000000011111111111
while (r - l > 1) {
int m = l + (r - l) / 2;
if (v[m] >= key)
r = m;
else
l = m;
}
return r; // l gives the first 0 element and r gives the first 1 element
}
int LIS(vector<int> &v)
{
if (v.size() == 0)
return 0;
vector<int> tail(v.size(), 0);
int length = 1; // always points empty slot in tail
tail[0] = v[0];
for (int i = 1; i < v.size(); i++)
{
if (v[i] < tail[0]) // new smallest value
tail[0] = v[i];
else if (v[i] > tail[length - 1]) // v[i] extends largest subsequence
tail[length++] = v[i];
else
tail[CeilIndex(tail, -1, length - 1, v[i])] = v[i];
// v[i] will become end candidate of an existing subsequence or
// Throw away larger elements in all LIS, to make room for upcoming greater
// elements than v[i]
// (and also, v[i] would have already appeared in one of LIS, identify the
// location and replace it)
}
return length;
}
int main() {
vector<int> v{2, 5, 3, 7, 11, 8, 10, 13, 6};
cout << "Length of Longest Increasing Subsequence is " << LIS(v) << endl;
return 0;
}