Cod sursa(job #672490)
#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;
}
}
sort( TopMax.begin(), TopMax.end() );
out<<TopMax.size()<<'\n';
for( it=TopMax.begin(), iend=TopMax.end(); it < iend; ++it )
out<<v[*it]<<' ';
out<<'\n';
return EXIT_SUCCESS;
}