Cod sursa(job #215716)

Utilizator lamez0rBogdan Bondor lamez0r Data 20 octombrie 2008 16:11:11
Problema Subsir 2 Scor 45
Compilator c Status done
Runda Arhiva de probleme Marime 0.77 kb
#include<stdio.h>
int n,v2[5001],lm;
long v[5001];

void read ()
	{
	FILE *f=fopen("subsir2.in","r");
	fscanf(f,"%d",&n);
	int i;
	for (i=1;i<=n;++i)
		fscanf(f,"%ld",&v[i]);
	fclose(f);
	}

void solve ()
	{
	int i,j,max;
	v2[n]=1;
	for (i=n-1;i>=1;--i)
		{
		max=0;
		for (j=i+1;j<=n;++j)
			if (v2[j]>=max&&v[j]>=v[i])
				max=v2[j];
		v2[i]=max+1;
		if (lm<v2[i])
			lm=v2[i];
		}
	}

void drum ()
	{
	FILE *f;
	f=fopen("subsir2.out","w");
	fprintf(f,"%d\n",lm);
	int t=0,p=1,i,bun;
	do 	{
		while (v2[p]!=lm||v[t]>v[p])
			p++;
		bun=p;
		for (i=p;i<=n;++i)
			if (lm==v2[i]&&v[i]<v[bun])
				bun=i;
		fprintf(f,"%d ",bun);
		t=bun;
		lm --;
		} while (lm);
	fclose(f);
	}

int main ()
{
read ();
solve ();
drum ();
return 0;
}