Cod sursa(job #736243)

Utilizator BitOneSAlexandru BitOne Data 18 aprilie 2012 11:08:51
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.25 kb
#include <vector>
#include <fstream>
#include <cstdlib>
#include <iterator>
#include <algorithm>
#define N_MAX 100011

using namespace std;

vector< int > v, vSort;
int f[N_MAX], l[N_MAX], aib[N_MAX], index[N_MAX];

void Update( const int& N, int xIndex, int xSIndex )
{
	for( ; xSIndex <= N; xSIndex+=( xSIndex & -xSIndex ) )
		if( l[aib[xSIndex]] < l[xIndex] )
			aib[xSIndex]=xIndex;
}
int Query( int xIndex )
{
	int mIndex=0;

	for( ; xIndex; xIndex-=( xIndex & -xIndex ) )
		if( l[aib[xIndex]] > l[mIndex] )
			mIndex=aib[xIndex];
	return mIndex;
}
inline void Output( ostream& out, int x )
{
	if( 0 == x )
		return ;
	Output( out, f[x] );
	out<<v[x]<<' ';
}
int main()
{
	int N, i, mIndex=0;
	ifstream in( "scmax.in" );
	ofstream out( "scmax.out" );

	in>>N;
	v.push_back(0);
	copy( istream_iterator<int>(in), istream_iterator<int>(), back_inserter(v) );
	vSort=v;
	sort( vSort.begin(), vSort.end() );
	for( i=1; i <= N; ++i )
		index[i]=lower_bound( vSort.begin(), vSort.end(), v[i] ) - vSort.begin();
	for( i=1; i <= N; ++i )
	{
		f[i]=Query( index[i]-1 );
		l[i]=1+l[f[i]];
		if( l[i] > l[mIndex] )
			mIndex=i;
		Update( N, i, index[i] );
	}
	out<<l[mIndex]<<'\n';
	Output( out, mIndex );
	return EXIT_SUCCESS;
}