Cod sursa(job #705462)

Utilizator BitOneSAlexandru BitOne Data 4 martie 2012 13:46:14
Problema Subsir crescator maximal Scor 55
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 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( 0 == 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);
	for( i=1; i < N; ++i )
	{
		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[TopMax[middle]] == v[i] )
			{
				left=middle;
				break;
			}
			else if( v[i] <= v[TopMax[middle]] )
					right=middle-1;
				 else left=middle+1; 
		}
		if( v[TopMax[left]] > v[i] )
		{
			if( left > 1 )
				f[i]=TopMax[left-1];
			TopMax[left]=i;
		}
	}
	
	out<<TopMax.size()<<'\n';
	Output( out, TopMax.back() );

	return EXIT_SUCCESS;
}