Pagini recente » Cod sursa (job #786896) | Cod sursa (job #2925801) | Cod sursa (job #178937) | Cod sursa (job #250837) | Cod sursa (job #349575)
Cod sursa(job #349575)
/*
* 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;
}