-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBOJ14003.cpp
79 lines (56 loc) · 1.23 KB
/
BOJ14003.cpp
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//BOJ14003
//https://www.geeksforgeeks.org/longest-monotonically-increasing-subsequence-size-n-log-n/
//https://seungkwan.tistory.com/8
//https://jason9319.tistory.com/113
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
int N;
vector<int> elements, L, P, ans;
void LIS() {
for (int i = 0; i < N; ++i) {
int lsize = L.size();
if (L.size() == 0) {
L.push_back(elements[i]);
P.push_back(1);
}
else if (L[lsize - 1] < elements[i]) {
L.push_back(elements[i]);
P.push_back(L.size());
}
else {
auto iter = lower_bound(L.begin(), L.end(), elements[i]);
*iter = elements[i];
P.push_back(iter - L.begin() + 1);
}
}
return;
}
void backtrack(int pidx, int lidx) {
if (pidx == 0) return;
if (P[pidx - 1] == lidx) {
ans.push_back(elements[pidx - 1]);
backtrack(pidx - 1, lidx - 1);
}
else backtrack(pidx - 1, lidx);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> N;
for (int i = 0; i < N; ++i) {
int t;
cin >> t;
elements.push_back(t);
}
LIS();
printf("%d\n", L.size());
int l = 1;
backtrack(N, L.size());
//sort(ans.begin(), ans.end());
for (int i = ans.size(); i > 0; --i) {
printf("%d ", ans[i - 1]);
}
printf("\n");
return 0;
}