Cod sursa(job #2660309)

Utilizator Radu_FilipescuFilipescu Radu Radu_Filipescu Data 18 octombrie 2020 20:52:44
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.08 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMAX = 100005;

int N;
int lis[NMAX];
int lgmax;
int a[NMAX];
int pre[NMAX];

void Read()
{
    fin >> N;
    for( int i = 1; i <= N; ++i )
        fin >> a[i];
}

int LowBound( int lf, int rg, int val ) {
    if( lf > rg ) return 0;

    int mid = lf + ( rg - lf ) / 2;

    if( a[lis[mid]] < val ) return max( mid, LowBound( mid + 1, rg, val ) );
    else return LowBound( lf, mid - 1, val );
}

void Remake( int pos ) {
    if( pos == 0 ) return;

    Remake( pre[pos] );
    fout << a[pos] << ' ';
}

void Do()
{
    for( int i = 1; i <= N; ++i ) {
        int p;

        if( lgmax == 0 ) p = 0;
        else p = LowBound( 1, lgmax, a[i] );

        pre[i] = lis[p];
        lis[p + 1] = i;

        if( p == lgmax ) ++lgmax;
    }

    //for( int i = 1; i <= lgmax; ++i )
    //    fout << a[lis[i]] << ' ';

    fout << lgmax << '\n';

    Remake( lis[lgmax] );
}

int main()
{
    Read();
    Do();

    return 0;
}