Pagini recente » Cod sursa (job #2255694) | Cod sursa (job #1338535) | Cod sursa (job #2791083) | Cod sursa (job #1567632) | Cod sursa (job #2512961)
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int NMAX = 100505;
const int INF = 2e9 + 5;
struct entry {
int value, originalPos;
bool operator < (const entry& other) const {
return value < other.value || (value == other.value && originalPos > other.originalPos);
}
};
int N, A[NMAX], D[NMAX], P[NMAX];
vector<entry> lowestWithLength;
int main() {
freopen("scmax.in", "r", stdin);
freopen("scmax.out", "w", stdout);
scanf("%d", &N);
lowestWithLength.assign(N + 1, {INF, INF});
lowestWithLength[0] = {-INF, 0};
int longestIncreasingPos = 0;
for (int i = 1; i <= N; i++) {
scanf("%d", &A[i]);
entry newEntry = {A[i], i};
int idxLgeq = lower_bound(lowestWithLength.begin(), lowestWithLength.end(), newEntry) - lowestWithLength.begin();
lowestWithLength[idxLgeq] = newEntry;
D[i] = idxLgeq;
P[i] = lowestWithLength[idxLgeq - 1].originalPos;
if (D[longestIncreasingPos] < D[i]) {
longestIncreasingPos = i;
}
}
vector<int> recons;
recons.reserve(D[longestIncreasingPos]);
int currentPos = N;
while (currentPos) {
recons.push_back(currentPos);
currentPos = P[currentPos];
}
reverse(recons.begin(), recons.end());
printf("%d\n", (int) recons.size());
for (auto& pos: recons) {
printf("%d ", A[pos]);
}
printf("\n");
return 0;
}