Cod sursa(job #1193137)

Utilizator ZenusTudor Costin Razvan Zenus Data 31 mai 2014 00:53:23
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.19 kb
#include <cstdio>
#include <algorithm>

using namespace std;

#define NMAX 100001
#define max(a,b) ((a<b) ? b : a)

int A[NMAX],Q[NMAX],P[NMAX],sol[NMAX];
int maxSol,maxLg,L,R,M,nowP,i,N;

int main()
{
    freopen("scmax.in","r",stdin);
    freopen("scmax.out","w",stdout);

    scanf("%d",&N);
    for (i=1;i<=N;++i) scanf("%d",&A[i]);

    Q[1]=A[1];
    maxLg=P[1]=1;

    for (i=2;i<=N;++i)
    {
        L=1;
        R=maxLg;
        nowP=0;
        while (L<=R && !nowP)
        {
            M=(L+R)/2;

            if (Q[M]==A[i])
            {
                nowP=M;
                continue;
            }

            (Q[M]>A[i]) ? R=M-1 : L=M+1;
        }

        if (nowP==0) nowP=L;

        Q[nowP]=A[i];
        maxLg=max(maxLg,L);

        P[i]=nowP;
        maxSol=max(maxSol,P[i]);
    }

    for (i=1;i<=N;++i) if (P[i]==maxSol) nowP=i;

    sol[1]=A[nowP];
    sol[0]=1;
    maxLg=P[nowP];

    for (i=nowP-1;i>=1;i--)
        {
            if (P[i]!=maxLg-1) continue;

            --maxLg;
            sol[++sol[0]]=A[i];
        }

    printf("%d\n",sol[0]);

    for (i=sol[0];i>=1;--i) printf("%d ",sol[i]);

    return 0;
}