Cod sursa(job #1046204)

Utilizator mucenic_b101Bogdan Mucenic mucenic_b101 Data 2 decembrie 2013 19:09:56
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.12 kb
#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;

}