-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14002.cpp
45 lines (45 loc) · 1012 Bytes
/
14002.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
#include <bits/stdc++.h>
using namespace std;
int n, ans;
int arr[1000];
int memo[1000];
int getLen(int start) {
if(start == n) return 0;
int &ret = memo[start];
if(ret != 0) return ret;
ret = 1;
for(int i = start + 1; i < n; i++) {
if(arr[start] < arr[i]) {
ret = max(ret, getLen(i) + 1);
}
}
return ret;
}
void print(int start) {
if(start == n) return;
int maxStart = -1;
int maxLen = 0;
for(int i = start + 1; i < n; i++) {
if(maxLen < getLen(i) && arr[start] < arr[i]) {
maxLen = getLen(i);
maxStart = i;
}
}
if(maxStart == -1) return;
cout << arr[maxStart] << " ";
print(maxStart);
}
int main() {
cin >> n;
for(int i = 0; i < n; i++) cin >> arr[i];
int start;
for(int i = 0; i < n; i++) {
if(getLen(i) > ans) {
start = i;
ans = getLen(i);
}
}
cout << ans << "\n";
cout << arr[start] << " ";
print(start);
}