Cod sursa(job #2565409)

Utilizator BogauuuBogdan Ivancu Bogauuu Data 2 martie 2020 14:10:36
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 kb
#include <fstream>

using namespace std;

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

int n,i,st,dr,mij,k,x,l,af[100005],t[100005],a[100005],d[100005];

int main()
{
    fin >> n >> a[1];
    d[1]=1;
    k=1;
    for (i=2;i<=n;i++)
    {
        fin >> a[i];
        st=1;
        dr=k;
        while (st<=dr)
        {
            mij=(st+dr)/2;
            if (a[d[mij]]<a[i]) st=mij+1;
            else dr=mij-1;
        }
        if (st==k+1) k++;
        d[st]=i;
        t[i]=d[st-1];
    }
    fout << k << "\n";
    x=d[k];
    while (x!=0)
    {
        af[++l]=a[x];
        x=t[x];
    }
    for (i=l;i>=1;i--) fout << af[i] << " ";

    return 0;
}