-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14003.cpp
48 lines (40 loc) · 978 Bytes
/
14003.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
#include <bits/stdc++.h>
using namespace std;
const int dy[4] = { -1,1,0,0 };
const int dx[4] = { 0,0,-1,1 };
int n, num, len;
int lis[1000001];
pair<int, int> ans[1000001];
const int INF = 1e9 + 1;
stack<int> st;
void init() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
int main() {
init();
fill(lis, lis + 1000001, INF);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> num;
auto pos = lower_bound(lis, lis + len, num);
int idx = (int)(pos - lis);
if (*pos == INF) len++;
*pos = num;
ans[i].first = idx;
ans[i].second = num;
}
cout << len << "\n";
for (int i = n - 1; i >= 0; i--) {
if (ans[i].first == len - 1) {
st.push(ans[i].second);
len--;
}
}
while (st.size()) {
cout << st.top() << " ";
st.pop();
}
return 0;
}