Cod sursa(job #2713068)

Utilizator AndreibatmanAndrei Croitoriu Andreibatman Data 27 februarie 2021 10:36:17
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.91 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int n,i,j,v[100010],best[100010],p[100010],sol[100010],maxi,m,poz,lg;
int cb(int x)
{
    int st=0,dr=lg+1,mij;
    while(dr-st>1)
    {
        mij=(st+dr)/2;
        if(best[mij]<x)
            st=mij;
        else dr=mij;
    }
    return dr;
}
void make_dp()
{
    best[1]=v[1];
    p[1]=1;
    lg=1;
    for(i=2;i<=n;i++)
        if(v[i]>best[lg])
            best[++lg]=v[i],p[i]=lg;
        else
        {
            poz=cb(v[i]);
            best[poz]=v[i];
            p[i]=poz;
        }
    fout<<lg<<'\n';
    poz=n;
    for(i=lg;i>=1;i--)
    {
        while(p[poz]!=i)
            poz--;
        sol[i]=v[poz];
    }
    for(i=1;i<=lg;i++)
        fout<<sol[i]<<" ";

}
int main()
{
    fin>>n;
    for(i=1;i<=n;i++)
        fin>>v[i];
    make_dp();

    return 0;
}