Cod sursa(job #2512957)

Utilizator radustn92Radu Stancu radustn92 Data 22 decembrie 2019 00:59:38
Problema Subsir crescator maximal Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.08 kb
#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;
}