Cod sursa(job #177460)

Utilizator amadaeusLucian Boca amadaeus Data 12 aprilie 2008 23:15:17
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.93 kb
#include <cstdio>
#include <vector>
#include <algorithm>

#define NX 100010

#define val first
#define ix second

using namespace std;

pair< int, int > t[ NX ];
int N, res, v[ NX ], a[ NX ], c[ NX ], d[ NX ], T[ NX ];

#define BSZ ( 1<<16 )
char buf[ BSZ ];
int p;

/*
int get() {
	int x = 0;

	while( *p < '0' || *p > '9' )
		if( !*(p++) )
			fread( buf, 1, BSZ - 1, stdin ) ? *(p = buf) : *p = '0';

	while( *p >= '0' && *p <= '9' ) {
		x = x * 10 + *p - '0';
		if( !*(p++) )
			fread( buf, 1, BSZ - 1, stdin ) ? *(p = buf) : *p = '@';
	}

	return x;
}
*/

int get() {
	int x = 0;

	while( buf[ p ] < '0' || buf[ p ] > '9' )
		if( ++p >= BSZ )
			fread( buf, 1, BSZ, stdin ), p = 0;

	while( buf[ p ] >= '0' && buf[ p ] <= '9' ) {
		x = x * 10 + buf[ p ] - '0';
		if( ++p >= BSZ )
			fread( buf, 1, BSZ, stdin ), p = 0;
	}
	return x;
}

void cit() {
	int i;

	p = BSZ;
	N = get();

	for( i = 1; i <= N; i++ )
		v[i] = get();
}

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;
}

void rez() {
	int i;

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

	//normalize
	sort( t+1, t+1+N );

	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;
}