Cod sursa(job #434899)

Utilizator alexandru92alexandru alexandru92 Data 6 aprilie 2010 18:16:42
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.27 kb
/* 
 * File:   main.cpp
 * Author: SpeeDemon
 *
 * Created on April 6, 2010, 6:05 PM
 */
#include <vector>
#include <cstdlib>
#include <fstream>
#include <iterator>

/*
 * 
 */
using namespace std;
vector< int > v, TopMax, p;
int main(int argc, char** argv) {

    int N, i, left, right, middle;
    ifstream in( "scmax.in" );
    in>>N;
    copy( istream_iterator<int>(in), istream_iterator<int>(), back_inserter(v) );
    p.resize( N );
    TopMax.push_back(0);
    for( i=1; i < N; ++i )
    {
        if( v[TopMax.back()] < v[i] )
        {
            p[i]=TopMax.back();
            TopMax.push_back(i);
            continue;
        }
        left=0; right=TopMax.size()-1;
        while( left < right )
        {
            middle=left+(right-left)/2;
            if( v[i] > v[TopMax[middle]] )
                left=middle+1;
            else right=middle;
        }
        if( v[TopMax[left]] > v[i] )
        {
            if( left > 0 )
                p[i]=TopMax[left-1];
            TopMax[left]=i;
        }
    }
    for( N=TopMax.size(), i=TopMax.back(); N--; i=p[i] )
        TopMax[N]=v[i];
    ofstream out( "scmax.out" );
    out<<TopMax.size()<<'\n';
    copy( TopMax.begin(), TopMax.end(), ostream_iterator<int>( out, " " ) );
    out<<'\n';
    return (EXIT_SUCCESS);
}