Cod sursa(job #1969254)

Utilizator borcanirobertBorcani Robert borcanirobert Data 18 aprilie 2017 12:54:40
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.23 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

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

const int MAX = 100005;
int N;
int a[MAX];
int best[MAX], t[MAX], l[MAX];
vector<int> r;
int lmax;

void Read();
void Solve();
int Binary_Search( int x );
void Write( int p );

int main()
{
    Read();
    Solve();

    fout << lmax << '\n';
    for ( int i = 1; i <= N; ++i )
        if ( best[i] == lmax )
        {
            Write(i);
            break;
        }

    fin.close();
    fout.close();
    return 0;
}

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

void Solve()
{
    int i;
    for ( i = 1; i <= N; ++i )
    {
        int pos = Binary_Search(a[i]);
        t[i] = l[pos];
        l[pos + 1] = i;
        best[i] = pos + 1;

        lmax = max( lmax, pos + 1 );
    }
}

int Binary_Search( int x )
{
    int st = 1, dr = lmax, mij, r{0};

    while ( st <= dr )
    {
        mij = ( st + dr ) / 2;

        if ( a[l[mij]] < x )
        {
            r = mij;
            st = mij + 1;
        }
        else
            dr = mij - 1;
    }

    return r;
}

void Write( int p )
{
    if ( p == 0 )
        return;
    Write(t[p]);

    fout << a[p] << ' ';
}