-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlis.cpp
More file actions
36 lines (32 loc) · 856 Bytes
/
Copy pathlis.cpp
File metadata and controls
36 lines (32 loc) · 856 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
// Given a list of numbers of length n, this
// returns the longest increasing subsequence.
//
// Running time: O(n log n)
//
// INPUT: a vector of integers
// OUTPUT: a vector containing the LIS
#define STRICTLY_INCREASNG
vll LongestIncreasingSubsequence(vll v) {
vpll best; vll dad(v.size(), -1);
for (int i = 0; i < v.size(); i++) {
#ifdef STRICTLY_INCREASNG
pll item = mp(v[i], 0);
auto it = lower_bound(all(best), item);
item.ss = i;
#else
pll item = mp(v[i], i);
auto it = upper_bound(all(best), item);
#endif
if (it == best.end()) {
dad[i] = best.empty() ? -1 : best.back().ss;
best.pb(item);
} else {
dad[i] = it==best.begin() ? -1:prev(it)->ss;
*it = item;
}
}
vll ret;
for (int i = best.back().ss; i >= 0; i = dad[i])
ret.pb(v[i]);
reverse(all(ret)); return ret;
}