Pagini recente » Cod sursa (job #3358721) | Cod sursa (job #1926847) | Monitorul de evaluare | Cod sursa (job #2617028) | Cod sursa (job #1046204)
#include <cstdio>
#include<stack>
using namespace std;
int v[100001], best[100001], last[100001], sol[100001];
int main () {
FILE *f, *g;
f = fopen( "scmax.in", "r" );
g = fopen( "scmax.out", "w" );
int n, max, elmax = 0, poz, p, psol;
fscanf( f, "%d", &n );
for( int i = 0 ; i < n ; ++i )
fscanf( f, "%d", &v[i] );
best[0] = 1;
last[0] = 0;
elmax = -1;
for( int i = 1 ; i < n ; ++i ) {
max = 0;
p = i;
for( int j = i - 1 ; j >= 0 ; --j ) {
if( best[j] > max && v[j] < v[i]) {
max = best[j];
p = j;
}
best[i] = max + 1;
last[i] = p;
if( best[i] > elmax )
elmax = best[i], poz = i;
}
}
psol = 0;
int k = best[poz];
while( k ) {
sol[psol] = v[poz];
psol++;
--k;
poz = last[poz];
}
fprintf( g, "%d\n", elmax );
for( int i = psol - 1 ; i >= 0 ; --i )
fprintf( g, "%d ", sol[i] );
fclose( f );
fclose( g );
return 0;
}