Cod sursa(job #535065)

Utilizator BitOneSAlexandru BitOne Data 16 februarie 2011 18:42:14
Problema Subsir crescator maximal Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.24 kb
#include <vector>
#include <fstream>
#include <cstdlib>
#include <iterator>
#include <algorithm>

using namespace std;
vector< int > v, TopMax, f;
inline void output( ostream& out, int x )
{
    if( x )
    {
        output( out, f[x] );
        out<<x<<'\n';
    }
}
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.push_back(0);
    for( i=1; i < N; ++i )
    {
        left=0; right=TopMax.size()-1;
        while( left < right )
        {
            middle=(left+right)>>1;
            if( v[TopMax[middle]] == v[i] )
            {
                left=middle;
                continue;
            }
            if( v[TopMax[middle]] > v[i] )
                right=middle;
            else left=middle+1;
        }
        f.push_back(0);
        if( v[TopMax[left]] > v[i] )
        {
            if( left >= 1 )
                f[i]=TopMax[left];
            TopMax[left]=i;
        }
        else TopMax.push_back(i);
    }
    ofstream out( "scmax.out" );
    out<<TopMax.size()<<'\n';
    output( out, TopMax.back() );
    return EXIT_SUCCESS;
}