Cod sursa(job #672491)

Utilizator BitOneSAlexandru BitOne Data 2 februarie 2012 12:03:08
Problema Subsir crescator maximal Scor 65
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 kb
#include <vector>
#include <fstream>
#include <cstdlib>
#include <iterator>
#include <algorithm>

using namespace std;
typedef unsigned int uint;

vector< uint > v, TopMax;
vector< uint >::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<uint>(in), istream_iterator<uint>(), back_inserter(v) );
	TopMax.push_back( 0 );
	for( i=1; i < N; ++i )
	{
		if( v[TopMax.back()] < v[i] )
		{
			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] )
					TopMax[left]=i;
			 }
	}
	out<<TopMax.size()<<'\n';
	for( it=TopMax.begin(), iend=TopMax.end(); it < iend; ++it )
		out<<v[*it]<<' ';
	out<<'\n';
	
	return EXIT_SUCCESS;
}