-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongest-increasing-subsequence.tex
44 lines (40 loc) · 1.13 KB
/
longest-increasing-subsequence.tex
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
\subsection{Longest Increasing Subsequence}
\begin{lstlisting}[language=C++]
// Given a list of numbers of length n, this routine extracts a
// longest increasing subsequence.
//
// Running time: O(n log n)
//
// INPUT: a vector of integers
// OUTPUT: a vector containing the longest increasing subsequence
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<PII> VPII;
#define STRICTLY_INCREASNG
VI LongestIncreasingSubsequence(VI v) {
VPII best;
VI dad(v.size(), -1);
for (int i = 0; i < v.size(); i++) {
#ifdef STRICTLY_INCREASNG
PII item = make_pair(v[i], 0);
VPII::iterator it = lower_bound(best.begin(), best.end(), item);
item.second = i;
#else
PII item = make_pair(v[i], i);
VPII::iterator it = upper_bound(best.begin(), best.end(), item);
#endif
if (it == best.end()) {
dad[i] = (best.size() == 0 ? -1 : best.back().second);
best.push_back(item);
} else {
dad[i] = dad[it->second];
*it = item;
}
}
VI ret;
for (int i = best.back().second; i >= 0; i = dad[i])
ret.push_back(v[i]);
reverse(ret.begin(), ret.end());
return ret;
}
\end{lstlisting}