Cod sursa(job #3276849)

Utilizator nistor_dora_valentinaNistor Dora Valentina nistor_dora_valentina Data 14 februarie 2025 21:22:20
Problema Subsir crescator maximal Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.05 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=-1;
    while(st<=dr)
    {
        int mij=(st+dr)/2;
        if(best[mij]>=a[i])
        {
            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]);
            if(best[lg]>=a[i])
            {
                best[lg]=a[i];
                poz[i]=lg;
            }
        }
    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;
}