Cod sursa(job #2556510)

Utilizator SergiuS3003Sergiu Stancu Nicolae SergiuS3003 Data 24 februarie 2020 22:59:10
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f ( "scmax.in" );
ofstream g ( "scmax.out" );
const int NMAX = 100001;
int best[NMAX], L[NMAX], p[NMAX], v[NMAX], nr = 1;
void afis ( int i )
{
    if ( p[i] > 0 ) afis ( p[i] );

    g << v[i] << ' ';
}
int cautbin ( int x )
{
    int p = 0, u = nr, rasp = 0;

    while ( p <= u )
    {
        int m = ( p + u ) / 2;

        if ( v[L[m]] < x )
        {
            rasp = m;
            p = m + 1;
        }
        else u = m - 1;
    }

    return rasp;
}
int main()
{
    int n;
    f >> n;

    for ( int i = 1; i <= n; i++ )
        f >> v[i];

    L[1] = 1;
    L[0] = 0;
    best[1] = 1;

    for ( int i = 2; i <= n; i++ )
    {
        int poz = cautbin ( v[i] );
        p[i] = L[poz];
        L[poz + 1] = i;
        best[i] = poz + 1;

        if ( nr < ( poz + 1 ) )
            nr = poz + 1;
    }

    int mx = 0, poz;

    for ( int i = 1; i <= n; i++ )
        if ( best[i] > mx )
        {
            mx = best[i];
            poz = i;
        }

    g << mx << '\n';
    afis ( poz );
    return 0;
}