Cod sursa(job #227771)

Utilizator AndreiDDiaconeasa Andrei AndreiD Data 5 decembrie 2008 15:37:03
Problema Subsir crescator maximal Scor 70
Compilator c Status done
Runda Arhiva educationala Marime 0.85 kb
#include <stdio.h>

#define Nmax 103030


int V[Nmax],N,max,p,poz[Nmax],best[Nmax],i,j;


void read_data()
{
  freopen("scmax.in","r",stdin);
  scanf("%d\n", &N);
  for (i=1;i<=N;++i)
  scanf("%d ", &V[i]);
}


void find_max()
{
  best[N]=1;
  poz[N]=-1;
  max=0;
  p=N;
  for (i=N-1;i>=1;--i)
       {
	best[i]=1;
	poz[i]=-1;
	for (j=i+1;j<=N;++j)
	     {
	       if (V[i]<V[j] && best[i]<best[j]+1)
		   {
		    best[i]=best[j]+1;
		    poz[i]=j;
		    if (best[i]>max)
			{
			 max=best[i];
			 p=i;
			}
		   }
	      }
     }
}

void constr_sir()
{
   i=p;
   while (i!=-1)
   {
   printf("%d ", V[i]);
   i=poz[i];
   }
}


void write_data()
{
  freopen("scmax.out","w",stdout);
  printf("%d\n", max);
}

int main()
{
   read_data();
   find_max();
   write_data();
   constr_sir();
   return 0;
}