Pagini recente » Cod sursa (job #1842198) | Cod sursa (job #27923) | Cod sursa (job #1030361) | Cod sursa (job #710343) | Cod sursa (job #3259697)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int pos[100001];
int a[100001];
set<pair<int, int>> maxLen;
void output(int currentPos) {
if (pos[currentPos] == 0) {
fout << a[currentPos] << ' ';
return;
}
// cout << currentPos << ' ';
output(pos[currentPos]);
fout << a[currentPos] << ' ';
}
int main() {
int n;
fin >> n;
for (int i = 1; i <= n; ++i) {
fin >> a[i];
}
// int maxLen[100001];
int ansLen = 1, lastPos = 1;
for (int i = 1; i <= n; ++i) {
//maxLen[i] = 1;
maxLen.insert({1, i});
for (set<pair<int, int>>::reverse_iterator it = maxLen.rbegin(); it != maxLen.rend(); ++it) {
if (a[i] > a[it->second]) {
pos[i] = it->second;
if (ansLen < it->first + 1) {
ansLen = it->first + 1;
lastPos = i;
}
maxLen.erase({1, i});
maxLen.insert({it->first + 1, i});
break;
}
}
}
fout << ansLen << '\n';
output(lastPos);
return 0;
}