Pagini recente » Cod sursa (job #657981) | Cod sursa (job #441383) | Cod sursa (job #442951) | Cod sursa (job #1119184) | Cod sursa (job #1811251)
#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;
}