Cod sursa(job #535390)

Utilizator BitOneSAlexandru BitOne Data 17 februarie 2011 10:09:12
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.49 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 x )
{
    if( x >= 0 )
    {
        output( out, f[x] );
        out<<v[x]<<' ';
    }
}
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);
    f[0]=-1;
    for( i=1; i < N; ++i )
    {
        f[i]=-1;
        if( v[TopMax.back()] < v[i] )
        {
            f[i]=TopMax.back();
            TopMax.push_back(i);
            continue;
        }
        left=0; right=TopMax.size()-1;
        while( left < right )
        {
            middle=(left+right)/2;
            if( v[TopMax[middle]] == v[i] )
            {
                left=middle;
                break;
            }
            if( v[TopMax[middle]] > v[i] )
                right=middle;
            else left=middle+1;
        }
        if( v[i] == v[TopMax[left]] )
            continue;
        if( v[TopMax[left]] > v[i] )
        {
            if( left > 0 )
                f[i]=TopMax[left-1];
            TopMax[left]=i;
        }
        else TopMax.push_back(i);
    }
    ofstream out( "scmax.out" );
    out<<TopMax.size()<<'\n';
    output( out, TopMax.back() );
    out<<'\n';
    return EXIT_SUCCESS;
}