Cod sursa(job #350617)

Utilizator alexandru92alexandru alexandru92 Data 25 septembrie 2009 07:43:32
Problema Subsir crescator maximal Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.52 kb
/*
 * File:   main.cpp
 * Author: speedemon
 *
 * Created on September 19, 2009, 6:06 PM
 */
#include <vector>
#include <fstream>
#include <cstdlib>
#include <iterator>
#define InFile "scmax.in"
#define OutFile "scmax.out"

/*
 *
 */

using namespace std;
ifstream in;
ofstream out;
int main()

{int N,x,i,st,dr,m,pile,maxL=-1,poz,pozs;
    in.open(InFile);
    in>>N;
    vector<int> v,TopPile,b;
    vector<int> p( N, -1 ), Length( N, 1 );
    for( i=0; i < N; ++i ) in>>x,v.push_back(x);
    TopPile.push_back(0);
    for( i=1; i < N; ++i )
    {pile=-1;
        st=0; dr=TopPile.size();
        while( st < dr )
        {
            m=st+(dr-st)/2;
            if( v[TopPile[m]] < v[i] )
            {
                if( -1 == pile || Length[m] > Length[pile] )
                {
                    pile=m;
                }
                st=m+1;
            }
            else dr=m-1;
        }
        if( -1 == pile )
        {p[i]=i;
            TopPile.push_back(i);
            if( maxL < 1 ) maxL=1,poz=i;
        }
        else {
                p[i]=TopPile[pile];
                TopPile[pile]=i;
                ++Length[pile];
                if( maxL < Length[pile] ) maxL=Length[pile],poz=i;
             }
    }
    out.open(OutFile);
    out<<maxL<<'\n';
    if( 1 == maxL ) 
    {
        out<<v[poz];
        return EXIT_SUCCESS;
    }
    while( maxL-- )
    {
        pozs=poz;
        b.push_back( v[poz] );
        poz=p[poz]; 
    }
    copy( b.rbegin(), b.rend(), ostream_iterator<int>(out," ") );
    return EXIT_SUCCESS;
}