Cod sursa(job #2512961)

Utilizator radustn92Radu Stancu radustn92 Data 22 decembrie 2019 01:10:07
Problema Subsir crescator maximal Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.29 kb
#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;
}