Cod sursa(job #350841)

Utilizator alexandru92alexandru alexandru92 Data 26 septembrie 2009 09:27:33
Problema Subsir crescator maximal Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.63 kb
/*
 * File:   main.cpp
 * Author: speedemon
 *
 * Created on September 19, 2009, 6:06 PM
 */

#include <vector>
#include <fstream>
#include <cstdlib>
#include <iterator>
#include <algorithm>

/*
 *
 */

using namespace std;
ifstream in;
ofstream out;
vector<int> TopPile, Length, b;
int main()
{int N,i,pile,st,dr,m,maxL=1,poz=0,pozs;
 int *v,*p;
    in.open("roci.in");
    in>>N;
    v=(int*)calloc( N, sizeof(int) );
    p=(int*)calloc( N, sizeof(int) );
    for( i=0; i < N; ++i ) in>>v[i],p[i]=-1;
    TopPile.push_back(0); Length.push_back(1);
    for( i=1; i < N; ++i )
    {pile=-1;
        st=0; dr=TopPile.size()-1;
        while( st < dr )
        {
            m=st+(dr-st)/2;
            if( v[TopPile[m]] >= v[i] ) dr=m;
            else {st=m+1;
                      if( -1 == pile || Length[m] > Length[pile] )
                      {
                          pile=m;
                      }
                 }
        }
        if( -1 == pile )
        {
            TopPile.push_back(i);
            Length.push_back(1);
        }
        else {
                    p[i]=TopPile[pile];
                    TopPile[pile]=i;
                    ++Length[pile];
                    if( maxL < Length[pile] ) maxL=Length[pile],poz=i;
             }
    }
    out.open("roci.out");
    out<<maxL<<'\n';
    if( 1 == maxL )
    {
        out<<v[poz];
        free(v); free(p);
        return EXIT_SUCCESS;
    }
    while( -1 != poz )
    {
        b.push_back(v[poz]);
        pozs=poz;
        poz=p[poz];
        p[pozs]=-1;
    }
    copy( b.rbegin(), b.rend(), ostream_iterator<int>( out, " " ) );
    free(v); free(p);
    return EXIT_SUCCESS;
}