Cod sursa(job #2494523)

Utilizator BogdanGhGhinea Bogdan BogdanGh Data 17 noiembrie 2019 23:20:46
Problema Subsir crescator maximal Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#include <fstream>

using namespace std;
ifstream f("scmax.in");
ofstream g("scmax.out");
int a[100005],i,st[100005],k,n,sol[100005],poz,m;
int cbin(int l,int r,int x)
{
    int mij,poz=0;
    while(l<=r)
    {
        mij=(l+r)/2;
        if(x>=a[st[mij]])
        {
            poz=mij;
            l=mij+1;
        }
        else r=mij-1;
    }
    return poz;
}
int main()
{
    f>>n;
    for(i=1;i<=n;i++)
        f>>a[i];
    st[++k]=1;
    for(i=2;i<=n;i++)
    {
        if(a[i]>a[st[k]])
            {
                st[++k]=i;
                sol[i]=st[k-1];
            }
        else if(a[i]<=a[st[1]])
        st[1]=i;
        else {
            poz=cbin(1,k,a[i]);
            st[poz]=i;
            sol[i]=st[k-1];
        }
    }
    g<<k<<'\n';
    k=st[k];
    while(sol[k])
    {
        st[++m]=a[k];
        k=sol[k];
    }
    st[++m]=a[k];
    for(i=1;i<=m/2;i++)
        swap(st[i],st[m-i+1]);
    for(i=1;i<=m;i++)
        g<<st[i]<<' ';
    return 0;
}