Cod sursa(job #372837)

Utilizator alexandru92alexandru alexandru92 Data 11 decembrie 2009 20:53:28
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.26 kb
/* 
 * File:   main.cpp
 * Author: virtualdemon
 *
 * Created on December 11, 2009, 3:06 PM
 */
#include <vector>
#include <fstream>
#include <iterator>
#include <algorithm>
#define pb push_back

/*
 * 
 */
using namespace std;
vector<int> v, TopMax;
int main()
{int n, i, left, right, middle;
    ifstream in("scmax.in");
    in>>n;
    vector<int> prev( n, 0 );
    copy( istream_iterator<int>(in), istream_iterator<int>(), back_inserter(v) );
    TopMax.pb(0); 
    for( i=1; i < n; ++i )
    {
        if( v[TopMax.back()] < v[i] )
        {
            prev[i]=TopMax.back();
            TopMax.pb(i);
            continue;
        }
        left=0; right=TopMax.size()-1;
        while( left < right )
        {
            middle=left+(right-left)/2;
            if( v[TopMax[middle]] >= v[i] )
                right=middle;
            else left=middle+1;
        }
        if( v[TopMax[left]] > v[i] )
        {
            if( left > 0 )
                prev[i]=(TopMax[left-1]);//prev[i]=TopMax[left-1];
            TopMax[left]=i;
        }
    }
    for( n=TopMax.size(), i=TopMax.back(); n--; i=prev[i] )
        TopMax[n]=v[i];
    ofstream out("scmax.out");
    out<<TopMax.size()<<'\n';
    copy( TopMax.begin(), TopMax.end(), ostream_iterator<int>(out, " " ) );
    return 0;
}