Pagini recente » Cod sursa (job #221396) | Cod sursa (job #468066) | Cod sursa (job #252431) | Cod sursa (job #606790) | Cod sursa (job #1811253)
#include <bits/stdc++.h>
using namespace std;
int main() {
freopen("scmax.in", "r", stdin);
freopen("scmax.out", "w", stdout);
int n;
cin >> n;
vector<int> Stack, Index, Parent(n), V(n);
for(int i = 0; i < n; ++i) {
cin >> V[i];
auto pos = lower_bound(Stack.begin(), Stack.end(), V[i]) - Stack.begin();
Parent[i] = pos == 0 ? -1 : Index[pos - 1];
if(pos == Stack.size()) {
Stack.push_back(0);
Index.push_back(0);
}
Stack[pos] = V[i];
Index[pos] = i;
}
vector<int> Sol;
for(int ind = Index.back(); ind != -1; ind = Parent[ind])
Sol.push_back(ind);
cout << Sol.size() << endl;
for(auto it = Sol.rbegin(); it != Sol.rend(); ++it)
cout << V[*it] << " ";
return 0;
}