Cod sursa(job #350691)

Utilizator alexandru92alexandru alexandru92 Data 25 septembrie 2009 14:49:50
Problema Subsir crescator maximal Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.64 kb
/*
 * File:   main.cpp
 * Author: speedemon
 *
 * Created on September 19, 2009, 6:06 PM
 */
#include <vector>
#include <fstream>
#include <cstdlib>
#include <iterator>
#include <algorithm>
#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;
    vector<int> p( N, -1 ), Length( N, 1 );
    while( N-- )
    {
        in>>x;
        v.push_back(x);
    }
    TopPile.push_back(0);
    out.open(OutFile); 
    for( N=v.size(),i=1; i < N; ++i )
    {pile=0;
        st=0; dr=TopPile.size()-1;
        while( st < dr )
        {m=st+(dr-st)/2;
            if( v[TopPile[m]] < v[i] )
            {
                if( !pile || Length[m] > Length[pile] )
                {
                    pile=m;
                }
                if( v[TopPile[m+1]] >= v[i] ) dr=m-1;
                else st=m+1;
            }
            else  dr=m-1;
        } 
        if( !pile )
        {
            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<<maxL<<'\n';
    if( 1 == maxL ) out<<v[poz];
    else {vector<int> b;
             while( -1 != poz )
            {
                pozs=poz;
                b.push_back(v[poz]);
                poz=p[poz]; 
            }
            copy( b.rbegin(), b.rend(), ostream_iterator<int>(out," ") );
         }
    return EXIT_SUCCESS;
}