Cod sursa(job #392899)

Utilizator DraStiKDragos Oprica DraStiK Data 8 februarie 2010 15:55:54
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 kb
#include <algorithm>
using namespace std;

#define DIM 100005
int v[DIM],best[DIM],prec[DIM],lung[DIM];
int n,poz,ind,best_lg,MAX=1;

void read ()
{
    int i;

    scanf ("%d",&n);
    for (i=1; i<=n; ++i)
        scanf ("%d",&v[i]);
}

int cbin (int val)
{
    int st,dr,mij;

    for (st=0, dr=MAX; st<=dr; )
    {
        mij=st+(dr-st)/2;
        if (v[lung[mij]]<val && (val<v[lung[mij+1]] || mij==dr))
            return mij;
        else if (v[lung[mij]]<val)
            st=mij+1;
        else
            dr=mij-1;
    }
    return 0;
}

void print (int x)
{
    if (prec[x])
        print (prec[x]);
    printf ("%d ",v[x]);
}

void solve ()
{
    int i;

    best[1]=lung[1]=1;
    for (i=2; i<=n; ++i)
    {
        poz=cbin (v[i]);
        best[i]=poz+1;
        prec[i]=lung[poz];
        lung[poz+1]=i;
        MAX=max (MAX,poz+1);
        if (best[i]>best_lg)
        {
            best_lg=best[i];
            ind=i;
        }
    }
    printf ("%d\n",best_lg);
    print (ind);
}

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

    read ();
    solve ();

    return 0;
}