Cod sursa(job #1424700)

Utilizator laurageorgescuLaura Georgescu laurageorgescu Data 25 aprilie 2015 13:09:43
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.04 kb
#include<fstream>

using namespace std;

ifstream fin( "scmax.in" );
ofstream fout( "scmax.out" );

const int nmax = 100000;
const int inf = 2000000001;
int v[ nmax + 1 ], u[ nmax + 1 ];
int r[ nmax + 1 ];

void reconst( int pos ) {
    if ( pos == 0 ) {
        return ;
    }
    reconst( r[ pos ] );
    fout << v[ pos ] << " ";
}
int main() {
    int n, n2, p, ans = 0;
    fin >> n;
    for( int i = 1; i <= n; ++ i ) {
        fin >> v[ i ];
    }
    v[ 0 ] = inf;
    for( n2 = 1; (n2 << 1) <= n; n2 <<= 1 ) {
    }

    for( int i = 1; i <= n; ++ i ) {
        int sol = 0;
        for( int step = n2; step > 0; step >>= 1 ) {
            if ( sol + step < i && v[ i ] > v[ u[ sol + step ] ] ) {
                sol += step;
            }
        }

        r[ i ] = u[ sol ];
        if ( v[ u[ sol + 1 ] ] > v[ i ] || u[ sol + 1 ] == 0 ) {
            u[ sol + 1 ] = i;
        }

        if ( sol + 1 > ans ) {
            ans = sol + 1; p = i;
        }
    }
    fout << ans << "\n";
    reconst( p );
    fin.close();
    fout.close();
    return 0;
}