Cod sursa(job #534674)

Utilizator SheepBOYFelix Liviu SheepBOY Data 15 februarie 2011 23:01:22
Problema Subsir 2 Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.96 kb
#include<stdio.h>
#define MAX 5010

long long n,v[MAX],best[MAX],rec[MAX];
long long  FndMax(int pos)
{
	int a,max=-2000000;
	rec[pos]=0;
	a=pos--;
	while(pos>0)
	{
	/*	if(max < v[pos] && v[pos] < v[a] )
		{
			max=v[pos];
			rec[a]=pos;
		}*/
		if(max < best[pos] && v[pos] < v[a] )
		{
			max=best[pos];
			rec[a]=pos;
		}
		--pos;
	}
	
	if(rec[a]>-1)
		return best[rec[a]];
	else
		return 0;
}
int main()
{
	int i,j;
	

	
	freopen("subsir2.in","r",stdin);
	freopen("subsir2.out","w",stdout);
	
	scanf("%lld",&n);
	for(i=1;i<=n;++i)
		scanf("%lld",v+i);
	
	long long min=1000000;
	for(i=1;i<=n;++i)
		best[i]=1+FndMax(i);
	
	int ptr=n;
	int sol=best[n];
	int max=v[n];
	
	for(i=n-1;i>0;--i)
		if(v[i] > max)
		{
			max=v[i];
			if(best[i] < sol)
			{
				ptr=i;
				sol=best[i];
			}
		}
		printf("%d\n",sol);
	
	while(ptr)
	{
		printf("%d->%d ",ptr+1,v[ptr]);
		ptr=rec[ptr];
	}
		//reconstr sol
	return 0;
}