Cod sursa(job #1812123)

Utilizator Andrei_CotorAndrei Cotor Andrei_Cotor Data 21 noiembrie 2016 20:52:55
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 kb
#include<fstream>
using namespace std;
ifstream fi("scmax.in");
ofstream fo("scmax.out");
int n,A[100003],best[100003],P[100003],maxim,k,L[100003],nr,i,j,poz;

void afis(int i)
{
    if(P[i]>0)
        afis(P[i]);
    fo<<A[i]<<" ";
}

int caut(int x)
{
    int p,u,m;
    p=0;
    u=nr;
    m=(p+u)/2;
    while(p<=u)
    {
        if(A[L[m]]<x && A[L[m+1]]>=x)
            return m;
        else
            if (A[L[m+1]]<x)
            {
                p=m+1;
                m=(p+u)/2;
            }
            else
            {
                u=m-1;
                m=(p+u)/2;
            }
    }
    return nr;
}

int main()
{
    nr=1;
    fi>>n;
    for(i=1; i<=n; i++)
        fi>>A[i];
    best[1]=L[1]=1;
    L[0]=0;
    for(i=2; i<=n; i++)
    {
        poz=caut(A[i]);
        P[i]=L[poz];
        best[i]=poz+1;
        L[poz+1]=i;
        if(nr<poz+1)
            nr=poz+1;
    }
    maxim=0;
    poz=0;
    for(i=1; i<=n; i++)
        if(maxim<best[i])
        {
            maxim=best[i];
            poz=i;
        }
    fo<<maxim<<"\n";
    afis(poz);
    fo<<"\n";
    fi.close();
    fo.close();
    return 0;
}