47 lines
843 B
C++
47 lines
843 B
C++
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
|
|
|
|
int main(){
|
|
int N; cin >> N;
|
|
vector<int> a(N);
|
|
for (int i = 0; i < N; i++) {
|
|
cin >> a[i];
|
|
}
|
|
|
|
vector<int> d(N, 1);
|
|
vector<int> p(N, -1);
|
|
for (int i = 0; i < N; i++) {
|
|
for (int j = 0; j < i; j++) {
|
|
if (a[j] < a[i] && d[j] + 1 > d[i]) {
|
|
d[i] = d[j] + 1;
|
|
p[i] = j;
|
|
}
|
|
}
|
|
}
|
|
|
|
int ans = d[0], pos = 0;
|
|
for (int i = 1; i < N; i++) {
|
|
if (d[i] > ans) {
|
|
ans = d[i];
|
|
pos = i;
|
|
}
|
|
}
|
|
|
|
vector<int> subseq;
|
|
while (pos != -1) {
|
|
subseq.push_back(a[pos]);
|
|
pos = p[pos];
|
|
}
|
|
reverse(subseq.begin(), subseq.end());
|
|
|
|
cout << ans << endl;
|
|
for (int i = 0; i < ans; i++) {
|
|
if (i != 0) cout << " ";
|
|
cout << subseq[i];
|
|
}
|
|
cout << endl;
|
|
|
|
return 0;
|
|
} |