-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongest_increasing_subsequence.cpp
More file actions
59 lines (49 loc) · 1.1 KB
/
longest_increasing_subsequence.cpp
File metadata and controls
59 lines (49 loc) · 1.1 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
51
52
53
54
55
56
57
58
59
// BOJ 14003 가장 긴 증가하는 부분 수열 5
#include <bits/stdc++.h>
#define sz size()
#define bk back()
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
vector<int> lis(vector<int> &v) {
int n = v.sz;
vector<int> dp(n, 1);
vector<int> w = {v[0]};
for (int i = 1; i < n; i++) {
if (v[i] > w.bk) {
w.push_back(v[i]);
dp[i] = w.sz;
} else {
int j = lower_bound(w.begin(), w.end(), v[i]) - w.begin();
dp[i] = j + 1;
w[j] = min(w[j], v[i]);
}
}
vector<int> ret;
int i = n;
int j = w.sz;
while (j) {
if (dp[--i] == j) {
ret.push_back(v[i]);
j--;
}
}
reverse(ret.begin(), ret.end());
return ret;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; i++)
cin >> v[i];
vector<int> ans = lis(v);
cout << ans.sz << '\n';
for (int x : ans)
cout << x << ' ';
}