Pagini recente » Cod sursa (job #1088907) | Cod sursa (job #1510159) | Cod sursa (job #1744772) | Cod sursa (job #2972668) | Cod sursa (job #2512957)
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int NMAX = 100505;
const int INF = 2e9 + 5;
int N, A[NMAX], D[NMAX];
vector<int> lowestWithLength;
int main() {
freopen("ssm.in", "r", stdin);
// freopen("ssm.out", "w", stdout);
scanf("%d", &N);
lowestWithLength.assign(N + 1, INF);
lowestWithLength[0] = -INF;
int longestIncreasing = 0;
for (int i = 1; i <= N; i++) {
scanf("%d", &A[i]);
int idxLgeq = lower_bound(lowestWithLength.begin(), lowestWithLength.end(), A[i]) - lowestWithLength.begin();
lowestWithLength[idxLgeq] = A[i];
D[i] = idxLgeq;
longestIncreasing = max(longestIncreasing, idxLgeq);
}
vector<int> recons;
recons.reserve(longestIncreasing);
int currentPos = N, prev = INF;
while (currentPos) {
if (A[currentPos] < prev && D[currentPos] == longestIncreasing) {
recons.push_back(currentPos);
prev = A[currentPos];
longestIncreasing--;
}
currentPos--;
}
reverse(recons.begin(), recons.end());
printf("%d\n", (int) recons.size());
for (auto& pos: recons) {
printf("%d ", A[pos]);
}
printf("\n");
return 0;
}