Cod sursa(job #615067)
#include <vector>
#include <fstream>
#include <cstdlib>
#include <iterator>
#include <algorithm>
#define N_MAX 100011
using namespace std;
vector< int > v, TopMax;
int main( void )
{
int N, i, 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()] )
{
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]] )
TopMax[left]=i;
}
for( i=0, N=TopMax.size(); i < N; ++i )
TopMax[i]=v[TopMax[i]];
ofstream out( "scmax.out" );
out<<N<<'\n';
copy( TopMax.begin(), TopMax.end(), ostream_iterator<int>( out, " " ) );
out<<'\n';
return EXIT_SUCCESS;
}