Cod sursa(job #2576330)

Utilizator georgeoctavianGeorge Octavian Grumazescu georgeoctavian Data 6 martie 2020 18:36:07
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.28 kb
#include <bits/stdc++.h>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define ll long long
#define INF LLONG_MAX
using namespace std;
ifstream fin( "scmax.in" );
ofstream fout( "scmax.out" );
void fast()
{
    cin.tie( 0 );
    ios_base::sync_with_stdio( 0 );
}
int n;
ll a[100005];
ll dp[100005];
ll best[100005], v = 0;
vector<ll>sol;
ll ok( ll pos, ll x )
{
    if(best[pos]<x)
        return 1;
    return 0;
}
ll cauta(ll x)
{
    ll  l=0,r=v+1;
    while(l!=r)
    {
        ll mid=(l+r)/2;
        if(ok(mid,x))
            l=mid+1;
        else
            r=mid;
    }
    return l;
}
int main()
{
    fast();
    fin >> n;
    for( int i = 1; i <= n; i++ )
        fin >> a[i];
    best[1] = a[1];
    dp[1] = 1;
    v = 1;
    for( int i = 2; i <= n; i++ )
    {
        ll pos = cauta( a[i] );
        if( best[pos] == 0 )
            best[pos] = a[i];
        dp[i] = pos;
        best[pos]=a[i];
        v = max( v, pos );
    }
    fout << v << '\n';
    for( int i = n; i >= 1 && v > 0; i-- )
        if( v == dp[i] )
        {
            sol.pb( a[i] );
            v--;
        }
    reverse( sol.begin(), sol.end() );
    for( auto it : sol )
        fout << it << ' ';

    return 0;
}