Cod sursa(job #672500)

Utilizator BitOneSAlexandru BitOne Data 2 februarie 2012 12:29:38
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;
vector< int >::const_iterator it, iend;

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 );
	for( i=1; i < N; ++i )
	{
		if( v[TopMax.back()] < v[i] )
		{
			f[i]=TopMax.back();
			TopMax.push_back( i );
		}
		else {
				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;
				}
			 }
	}
	for( i=TopMax.back(), left=TopMax.size(); left; i=f[i], --left )
		TopMax[left-1]=v[ i ];	
	out<<TopMax.size() <<'\n';
	copy( TopMax.begin(), TopMax.end(), ostream_iterator<int>( out, " " ) );
	out<<'\n';
	
	return EXIT_SUCCESS;
}