Cod sursa(job #2474756)

Utilizator AndreiDeltaBalanici Andrei Daniel AndreiDelta Data 15 octombrie 2019 19:55:05
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.05 kb
#include <bits/stdc++.h>
#define Dim 100001
using namespace std;
ifstream f("scmax.in");
ofstream g("scmax.out");
int N,V[Dim],Q[Dim],L[Dim],lg,st,dr,gasit_poz,cnt,copie;
int Afis[Dim],again;

int main()
{
    f>>N;
    for(int i=1;i<=N;i++) f>>V[i];

    for(int i=1;i<=N;i++)
    {
        st=1; dr=lg;
        bool ok=0;
        while(st<=dr)
        {
            int mij=(st+dr)/2;
            if( V[ i ] > Q[ mij ] ) st=mij+1;
            else
            {
                gasit_poz=mij;
                ok=1;
                dr=mij-1;
            }
        }

        if( ok )
        {
            Q[ gasit_poz ] = V[ i ] ;
            L[ ++cnt ] = gasit_poz;
        }
        else
        {
            Q[ ++lg ] = V[ i ] ;
            cnt++;
            L[ cnt ] = lg;
        }

    }
    g<<lg<<'\n';
    copie=lg;

    for(int i=cnt;i >= 1 && copie > 0 ;i--)
    if( L[ i ] == copie )
    {
        copie--;
        Afis[++again]=i;
    }
    for(int i=again;i>=1;i--) g<<V[ Afis[ i ] ]<<" ";

    return 0;
}