Cod sursa(job #383796)

Utilizator mottyMatei-Dan Epure motty Data 18 ianuarie 2010 10:05:35
Problema Subsir crescator maximal Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include<stdio.h>

const int N=100005;

int n,v[N],a[N],pred[N],max,r[N],pz,baz;

void read()
{
    scanf("%d",&n);
    for( int i=1 ; i<=n ; ++i )
        scanf("%d",&a[i]);
}

void cb( int &N,int val )
{
    int i,step;
    for(step=1; step < N ; step<<=1 );
    for( i=0 ; step ; step>>=1 )
        if( i+step<N && a[v[i+step]] < a[val])
            i+=step;
    ++i;
    if(i > N)
        ++N;
    v[i] = val;
    pred[val]=i-1;
    /*
    if( a[v[i]] <= a[val] )
    {
        v[i]=val;
        if(i>max)
            max++;
        pred[val]=v[i-1];
    }
    else
    {
        v[i+1]=val;
        if( i+1 > max )
            max++;
        pred[val]=v[i];
    }
    */
}

void solve()
{
    for( int i=1 ; i<=n ; ++i )
        pred[i]=-1;
    max=1;
    for( int i=2 ; i<=n ; ++i )
        cb(max,i);
    int i=max;
    do{
        r[++pz]=a[i];
        i=pred[i];
    }
    while(  pred[i]!=-1 );
}

void print()
{
    printf("%d\n",pz);
    while( pz>0 )
    {
        printf("%d ",r[pz]);
        pz--;
    }
}

int main()
{
    freopen("scmax.in","r",stdin);
    freopen("scmax.out","w",stdout);
    read();
    solve();
    print();
    return 0;
}