-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
54 lines (44 loc) · 884 Bytes
/
main.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
#include <bits/stdc++.h>
#define MAX 1001
using namespace std;
int N, ans, ansCur;
int arr[MAX], dp[MAX][2];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N;
for (int i = 0; i < N; i++)
cin >> arr[i];
for (int i = 0; i < N; i++) {
dp[i][0] = 1;
dp[i][1] = i;
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < i; j++) {
if (arr[i] > arr[j]) {
if (dp[i][0] < dp[j][0] + 1) {
dp[i][0] = dp[j][0] + 1;
dp[i][1] = j;
}
}
}
if (ans < dp[i][0]) {
ans = dp[i][0];
ansCur = i;
}
}
vector<int> V;
int cur = ansCur;
while (cur != dp[cur][1]) {
V.push_back(cur);
cur = dp[cur][1];
}
V.push_back(cur);
cout << ans << "\n";
auto iter = V.end() - 1;
while (iter != V.begin()) {
cout << arr[*iter] << " ";
iter--;
}
cout << arr[*iter];
}