Cod sursa(job #349575)

Utilizator alexandru92alexandru alexandru92 Data 20 septembrie 2009 10:58:51
Problema Subsir crescator maximal Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 kb
/*
 * File:   main.cpp
 * Author: speedemon
 *
 * Created on September 19, 2009, 6:06 PM
 */

#include <fstream>
#include <cstdlib>
#define InFile "scmax.in"
#define OutFile "scmax.out"
/*
 *
 */

using namespace std;
ifstream in;
ofstream out;
int main()
{int N,i,j,max,lmax=-1,poz,pozj;
 int *v,*L,*p;
    in.open(InFile);
    in>>N;
    v=(int*)calloc( N, sizeof(int) );
    L=(int*)calloc( N, sizeof(int) );
    p=(int*)calloc( N, sizeof(int) );
    for( i=0; i < N; ++i ) in>>v[i],L[i]=1,p[i]=-1;
    for( i=N-2; i >= 0; --i )
    {pozj=max=-1;
        for( j=i+1; j < N; ++j )
            if( v[i] < v[j] && max < L[j] ) max=L[j],pozj=j;
        L[i]+=max; p[i]=pozj;
        if( L[i] > lmax ) lmax=L[i],poz=i;
    }
    out.open(OutFile);
    out<<lmax<<'\n';
    while( -1 != poz )
    {
        out<<v[poz]<<' ';
        pozj=poz;
        poz=p[poz];
        p[pozj]=-1;
    }
    free(v); free(L); free(p);
    return EXIT_SUCCESS;
}