Cod sursa(job #3276852)

Utilizator nistor_dora_valentinaNistor Dora Valentina nistor_dora_valentina Data 14 februarie 2025 21:31:22
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.99 kb
#include <fstream>

using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int n, a[100005], best[100005], i, poz[100005], lg, val, k, sol[100005];
int cb(int x)
{
    int st=1, dr=lg, p=0;
    while(st<=dr)
    {
        int mij=(st+dr)/2;
        if(best[mij]>=x)
        {
            dr=mij-1;
            p=mij;
        }
        else
            st=mij+1;
    }
    return p;
}
int main()
{
    fin>>n;
    for(i=1; i<=n; i++)
        fin>>a[i];
    best[1]=a[1];
    poz[1]=1;
    lg=1;
    for(i=2; i<=n; i++)
        if(best[lg]<a[i])
        {
            best[++lg]=a[i];
            poz[i]=lg;
        }
        else
        {
            int p=cb(a[i]);
            best[p]=a[i];
            poz[i]=p;
        }
    val=lg;
    for(i=n; i>=1; i--)
        if(poz[i]==val)
        {
            sol[val]=a[i];
            val--;
        }
    fout<<lg<<'\n';
    for(i=1; i<=lg; i++)
        fout<<sol[i]<<" ";
    return 0;
}