Cod sursa(job #615070)

Utilizator BitOneSAlexandru BitOne Data 8 octombrie 2011 15:11:52
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 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;
int main( void )
{
	int N, i, j, left, middle, right;
	ifstream in( "scmax.in" );
	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[i] > v[TopMax.back()] )
		{
			f[i]=TopMax.back();
			TopMax.push_back(i);
			continue;
		}
		left=0; right=TopMax.size();
		while( left < right )
		{
			middle=(left+right)>>1;
			if( v[i] == v[TopMax[middle]] )
			{
				left=middle;
				break;
			}
			if( v[i] < v[TopMax[middle]] )
				right=middle;
			else left=middle+1;
		}
		if( v[i] == v[TopMax[left]] )
			continue;
		if( v[i] < v[TopMax[left]] )
		{
			if( left )
				f[i]=TopMax[left-1];
			TopMax[left]=i;
		}
	}
	for( i=0, j=TopMax.back(), N=TopMax.size(); i < N; ++i, j=f[j] )
		TopMax[i]=v[j];
	ofstream out( "scmax.out" );
	out<<N<<'\n';
	copy( TopMax.rbegin(), TopMax.rend(), ostream_iterator<int>( out, " " ) );
	out<<'\n';
	return EXIT_SUCCESS;
}