Cod sursa(job #177440)

Utilizator amadaeusLucian Boca amadaeus Data 12 aprilie 2008 22:40:13
Problema Subsir crescator maximal Scor 70
Compilator c Status done
Runda Arhiva educationala Marime 1.37 kb
#include <stdio.h>
#include <stdlib.h>

#define NX 100010

struct ent {
	int val, ix;
};

typedef struct ent ent;

ent t[ NX ];
int N, res, v[ NX ], a[ NX ], c[ NX ], d[ NX ], T[ NX ];

void cit() {
	int i;
	
	scanf( "%d", &N );

	for( i = 1; i <= N; i++ )
		scanf( "%d", &v[i] );
}

void upd( int x, int y ) {
	for( ; x <= N; x += x & -x )
		if( c[ T[x] ] < c[ y ] )
			T[x] = y;
}

int query( int x ) {
	int res = 0;
	
	for( ; x; x -= x & -x )
		if( c[ T[x] ] > c[ res ] )
			res = T[x];
	return res;
}

int cmp( const void* x, const void *y ) {
	return ((ent *)x)->val - ((ent *)y)->val;
}

void rez() {
	int i;

	for( i= 1; i <= N; i++ )
		t[i].val = v[i], t[i].ix = i;

	//normalize
	qsort( t+1, N, sizeof( ent ), cmp );

	t[0].val = -1;
	for( i = 1; i <= N; i++ )
		a[ t[i].ix ] = (t[i].val == t[i-1].val) ? a[ t[i-1].ix ] : a[ t[i-1].ix ] + 1;

	for( i = 1; i <= N; i++ ) {
		d[i] = query( a[i]-1 );
		c[i] = c[ d[i] ] + 1;
		upd( a[i], i );
	}

	for( i = 1, res = 0; i <= N; i++ )
		res = c[res] < c[i] ? i : res;

	for( i = res, N = 1; i; i = d[i], N++ )
		a[ N ] = v[i];
}

void scr() {
	int i;
	
	printf( "%d\n", c[ res ] );

	for( i = N-1; i; i-- )
		printf( "%d ", a[i] );
}

int main() {
	freopen( "scmax.in", "r", stdin );
	freopen( "scmax.out", "w", stdout );

	cit();
	rez();
	scr();

	return 0;
}