Cod sursa(job #340991)

Utilizator DraStiKDragos Oprica DraStiK Data 17 august 2009 11:30:13
Problema Subsir 2 Scor 61
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <stdio.h>

#define DIM 5005

int a[DIM],best[DIM],urm[DIM];
int n,nrmin,ind;

void read ()
{
    int i;

    scanf ("%d",&n);
    for (nrmin=DIM, i=1; i<=n; ++i)
    {
	scanf ("%d",&a[i]);
	if (a[i]<nrmin)
	{
	    nrmin=a[i];
	    ind=i;
	}
    }
}

void print (int val)
{
    printf ("%d ",val);
    if (urm[val]!=val)
        print (urm[val]);
}

void solve ()
{
    int i,j,imin,min,nmin;

    for (i=n; i>=1; --i)
    {
        for (best[i]=1,urm[i]=i, min=nmin=DIM, j=i+1; j<=n; ++j)
            if (a[j]>a[i] && a[j]<nmin)
            {
                if (best[j]<min)
                {
                    min=best[j];
                    imin=j;
                }
                else if (best[j]==min && a[j]<a[imin])
                    imin=j;
                if (a[j]<nmin)
                    nmin=a[j];
            }
        if (min!=DIM)
        {
            best[i]=min+1;
            urm[i]=imin;
        }
    }
    printf ("%d\n", best[ind]);
    print (ind);
}

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

    read ();
    solve ();

    return 0;
}