Pagini recente » Cod sursa (job #2264811) | Cod sursa (job #582455) | Cod sursa (job #2321009) | Cod sursa (job #623617) | Cod sursa (job #3285304)
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream f("scmax.in");
ofstream g("scmax.out");
int n, a[100005], poz;
vector <int> ans, aux, pozitii, precedent;
int main()
{
f >> n;
for (int i = 1; i <= n; ++i) {
f >> a[i];
}
aux.push_back(a[1]);
pozitii.push_back(1);
precedent.resize(n + 1, -1);
for (int i = 2; i <= n; ++i) {
if (a[i] > aux.back()) {
aux.push_back(a[i]);
pozitii.push_back(i);
precedent[i] = pozitii[(int) pozitii.size() - 2];
}
else {
poz = lower_bound(aux.begin(), aux.end(), a[i]) - aux.begin();
aux[poz] = a[i];
pozitii[poz] = i;
if (poz > 0) {
precedent[i] = pozitii[poz - 1];
}
}
}
int ultimul = pozitii.back();
while (ultimul != -1) {
ans.push_back(a[ultimul]);
ultimul = precedent[ultimul];
}
g << (int) ans.size() << '\n';
reverse(ans.begin(), ans.end());
for (auto it : ans) {
g << it << ' ';
}
return 0;
}