Cod sursa(job #1974384)

Utilizator ArctopusKacso Peter-Gabor Arctopus Data 27 aprilie 2017 14:43:11
Problema Subsir crescator maximal Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.35 kb
#include <iostream>
#include <fstream>
#include <stack>
#include <vector>

using namespace std;

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

const int NLIM = 1e5 + 10;

int N;
int v[NLIM];
int el[NLIM];
vector< int > b;

int main()
{
    fin >> N;
    for( int i = 0; i < N; ++i )
        fin >> v[i];

    b.push_back( 0 );

    for( int i = 1; i < N; ++i )
    {
        if( v[i] > v[b.back()] )
        {
            el[i] = b.back();
            b.push_back( i );
        }
        else
        {
            // binary search
            int l = 0, r = b.size() - 1;
            while( l < r )
            {
                int m = ( l + r ) / 2;
                if( v[b[m]] > v[i] )
                {
                    r = m;
                }
                else
                    l = m + 1;
            }

            // l
            if( v[b[l]] > v[i] )
            {
                b[l] = i;
                if( l > 0 )
                    el[i] = b[l - 1];
            }
        }
    }

    stack< int > res;

    int x = b.back();
    while( res.size() < b.size() )
    {
        res.push( v[x] );
        x = el[x];
    }
    fout << b.size() << "\n";
    while( res.size() )
    {
        fout << res.top() << " ";
        res.pop();
    }
    fout << "\n";

    return 0;
}