Cod sursa(job #161789)

Utilizator rethosPaicu Alexandru rethos Data 18 martie 2008 20:07:34
Problema Nivele Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <stdio.h>
FILE *f=fopen("nivele.in","rt");
FILE *g=fopen("nivele.out","wt");
#define NMAX 50001
struct nod
	{ long val;
	  nod *urm;
	};
long n,a[NMAX];
int fct()
{ long i;
  for (i=1;i<=n;i++) if (a[i]>n) return 0;
  long k=1;
  nod *x=new nod;
  nod *p,*prim=x;
  x->val=1;
  x->urm=NULL;
  while (x!=NULL)
	{ if (x->val>a[k])
         	{while(x)
                    {p=x;
                     x=x->urm;
                     delete p;
                    }
                 return 0;
	  if (x->val==a[k])
            {p=x;
             x=x->urm;
             k++;
             delete p;
            }
	  else
		{ nod *y=new nod;
		  x->val++;
		  y->val=x->val;
		  y->urm=x->urm;
		  x->urm=y;
		}
	}
  if (k==n+1) return 1;
  return 0;
}
int main()
{ long t,k,i;
  fscanf(f,"%ld",&t);
  for (k=1;k<=t;k++)
	{ fscanf(f,"%ld",&n);
	  for (i=1;i<=n;i++) fscanf(f,"%ld",&a[i]);
	  if (fct()) fprintf(g,"DA\n");
		else fprintf(g,"NU\n");
	}
  fclose(f);
  fclose(g);
  return 0;
}