Cod sursa(job #182952)

Utilizator andrei_h5n1Haidau Andrei andrei_h5n1 Data 21 aprilie 2008 15:53:30
Problema Nivele Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <stdio.h>

struct nod
{
	long long unsigned inf;
	nod *prev, *next;
};
nod *p, *in, *sf;
long long unsigned t, n, x, i, j;

void sterge_lista()
{
	p=in;
	while(p)
	{
		nod *aux=p;
		p=p->next;
		delete aux;
	}
}
void add(int x)
{
	if(!in)
	{
		in=new nod;
		in->inf=x;
		in->next=0;
		in->prev=0;
		sf=in;
	}
	else
	{
		p=new nod;

		p->inf = x;
		p->next = 0;
		p->prev = sf;
		sf->next = p;
		sf=p;
	}
}
int elimin()
{
    if(sf && sf->prev)
	if(sf->inf==sf->prev->inf)
	{
		nod *aux1, *aux2, *aux3=new nod;

		aux1=sf;
		aux2=sf->prev;

		aux3->inf = sf->inf - 1;
		aux3->next = 0;
		aux3->prev = sf->prev->prev;

		if(!aux3->prev)  // daca am avut 2 elem in lista
		{
			in=aux3;
			sf=aux3;
		}
		else
		{
			sf = sf->prev->prev;
			sf->next = aux3;
			sf=sf->next;
		}
		//aux1->inf=aux1->next=aux1->prev=NULL;
		//aux2->inf=aux2->next=aux2->prev=NULL;
		delete aux1;
		delete aux2;

		return 1;
	}

		return 0;
}
int main()
{
	freopen("nivele.in", "r", stdin);
	freopen("nivele.out", "w", stdout);

	scanf("%llu", &t);

	for(i=1; i<=t; i++)
	{
		scanf("%llu", &n);
		in=NULL;
		sf=NULL;
		for(j=1; j<=n; j++)
		{
			scanf("%llu", &x);
			add(x);
			while(elimin());
		}
		p=in;
		if(!p->next && p->inf==1)
			printf("DA\n");
		else
			printf("NU\n");

		sterge_lista();
	}

	return 0;
}