Cod sursa(job #705465)

Utilizator BitOneSAlexandru BitOne Data 4 martie 2012 13:50:54
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include <vector>
#include <fstream>
#include <cstdlib>
#include <iterator>
#include <algorithm>
#define N_MAX 100011

using namespace std;

int f[N_MAX];
vector< int > v, TopMax;

inline void Output( ostream& out, int index )
{
	if( -1 == f[index] )
		out<<v[index]<<' ';
	else {
			Output( out, f[index] );
			out<<v[index]<<' ';
		 }
}
int main()
{
	int N, i, left, middle, right;
	ifstream in( "scmax.in" );
	ofstream out( "scmax.out" );

	in>>N;
	copy( istream_iterator<int>(in), istream_iterator<int>(), back_inserter(v) );
	TopMax.push_back(0);
	f[0]=-1;
	for( i=1; i < N; ++i )
	{
		f[i]=-1;
		if( v[TopMax.back()] < v[i] )
		{
			f[i]=TopMax.back();
			TopMax.push_back(i);
		}
		left=0; right=TopMax.size()-1;
		while( left <= right )
		{
			middle=(left+right)>>1;
			if( v[i] <= v[TopMax[middle]] )
				right=middle-1;
			else left=middle+1; 
		}
		if( v[TopMax[left]] > v[i] )
		{
			if( left )
				f[i]=TopMax[left-1];
			TopMax[left]=i;
		}
	}
	
	out<<TopMax.size()<<'\n';
	Output( out, TopMax.back() );

	return EXIT_SUCCESS;
}