-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboj14003.cpp
More file actions
58 lines (51 loc) · 1.25 KB
/
boj14003.cpp
File metadata and controls
58 lines (51 loc) · 1.25 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
#include <iostream>
#include <algorithm>
#include <stack>
using namespace std;
using p = pair<int, int>;
const int MAX = 1e6;
int arr[MAX];
int path[MAX];
vector<p> v;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int N;
cin >> N;
for (int i = 0; i < N; ++i) {
cin >> arr[i];
auto it = lower_bound(v.begin(), v.end(), p(arr[i], 0), [](p a, p b) {
return a.first < b.first;
});
if (it == v.end()) {
int prevIndex;
if (it != v.begin()) {
prevIndex = v.back().second;
} else {
prevIndex = -1;
}
v.emplace_back(arr[i], i);
path[i] = prevIndex;
} else {
*it = p(arr[i], i);
if (it != v.begin()) {
it--;
path[i] = (*it).second;
} else {
path[i] = -1;
}
}
}
int curr = v.back().second;
stack<int> pathStack;
while (curr != -1) {
pathStack.push(arr[curr]);
curr = path[curr];
}
cout<<pathStack.size()<<'\n';
while(!pathStack.empty()){
cout<<pathStack.top()<<' ';
pathStack.pop();
}
return 0;
}