Cod sursa(job #487264)

Utilizator AndreiRSStatescu Andrei Rares AndreiRS Data 24 septembrie 2010 16:53:10
Problema Subsir crescator maximal Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include <stdio.h>
#define DIM 100001

int N, A[DIM], X[DIM], T[DIM], Sol[DIM];

void cit () {
	scanf ("%d", &N);
	for (int i=1; i<=N; ++i)
		scanf ("%d", &A[i]);
}

int cbin (int NR) {
	int p=1, u=X[0], m;
	while (p <= u) {
		m = p+(u-p)/2;
		if (NR > A[X[m]])
			p = m+1;
		else
			u = m-1;
	}
	return p;
}

void scm () {
	int i, poz;
	X[0] = X[1] = 1;
	
	for (i=2; i<=N; ++i) {
		poz = cbin (A[i]);
		X[poz] = i;
		if (poz > X[0]) X[0] = poz;
		T[i] = X[poz-1];		
	}
}

void afs () {
	int poz = X[X[0]];
	while (T[poz]) {
		Sol[++Sol[0]] = poz;
		poz = T[poz];
	}
	
	printf ("%d\n", X[0]);
	for (int i=Sol[0]; i>0; --i)
		printf ("%d ", A[Sol[i]]);
}

int main () {
	freopen ("scmax.in", "r", stdin);
	freopen ("scmax.out", "w", stdout);
	
	cit ();
	scm ();
	afs ();
	
	return 0;
}