Cod sursa(job #809228)

Utilizator ahmed.abdraboahmed.abdrabo ahmed.abdrabo Data 8 noiembrie 2012 03:13:32
Problema Subsir crescator maximal Scor 25
Compilator cpp Status done
Runda Arhiva educationala Marime 0.64 kb
#include <cstdio>
#include <set>
#include <map>
using namespace std;

int P[1000000];

inline int next_int() {
	int d;
	scanf("%d", &d);
	return d;
}

void dfs(int x) {
	if (P[x] >= 0) {
		dfs(P[x]);
	}
	printf("%d ", x);
}

int main() {
	freopen("scmax.in", "r", stdin);
	freopen("scmax.out", "w", stdout);
	multiset<int> S;
	multiset<int>::iterator it;
	int n = next_int();
	while (n--) {
		int x = next_int();
		S.insert(x);
		it = S.lower_bound(x);
		if (it == S.begin())
			P[x] = -1;
		else {
			--it;
			P[x] = *it;
			++it;
		}
		if (++it != S.end())
			S.erase(it);
	}

	printf("%d\n", int(S.size()));
	int x = *S.rbegin();
	dfs(x);
	return 0;
}