forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLIS_n2.cpp
More file actions
44 lines (40 loc) · 723 Bytes
/
LIS_n2.cpp
File metadata and controls
44 lines (40 loc) · 723 Bytes
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
#include <iostream>
#include <vector>
using namespace std;
int n;
void Run() {
vector<int> a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
vector<int> mm(n, -1);
vector<int> next(n, -1);
for (int i = n - 1; i >= 0; --i) {
int res = 0, idx = -1;
for (int j = i + 1; j < n; ++j) {
if (a[j] > a[i] && mm[j] > res) {
res = mm[j], idx = j;
}
}
mm[i] = res + 1;
if (idx != -1) next[i] = idx;
}
int ret = 0, idx = -1;
for (int i = 0; i < n; ++i) {
if (mm[i] > ret) {
ret = mm[i], idx = i;
}
}
cout << ret << endl;
cout << a[idx];
while (1) {
idx = next[idx];
if (idx == -1) break;
cout << ' ' << a[idx];
}
cout << endl;
}
int main() {
while (cin >> n) {
Run();
}
return 0;
}