Cod sursa(job #441641)

Utilizator lavrsssUrsu Lilian lavrsss Data 13 aprilie 2010 00:33:54
Problema Gutui Scor 20
Compilator c Status done
Runda teme_upb Marime 4.39 kb
#include <stdio.h>
#include <stdlib.h>
typedef struct nod {
	long int value;
	struct nod *next;
}Nod;

typedef Nod* List;
long int n,hmax,u,rangs;
//calculez suma elementelor din lista
long int sum(List l)
{
 Nod *first;
 long int s=0;
 first = l;
    while (l) {
	s += (l) -> value;
	(l) = (l) -> next;
   }
 //  printf("suma merge, s= %ld\n",s);
 l = first;
	return s;
}
//elimin primul element din lista
long int delfirst(List *l)
{
  long int val;
  Nod *aux;
	val = (*l) -> value;
	aux = *l;
//	printf("Am sters un element\n");
	(*l) = (*l) -> next;		
	free(aux); 

 return val;	
}
//calculez lungimea listei
long int lung(List l)
{
  long int lng = 0;
  Nod *first;
	first = l;
	while (l)
	{
		lng++;
		l = l ->next;
	}

	l = first;
 return lng;	
}
//sterg ultimul element din lista (cu cea mai mica greutate)
long int dellast(List *l)
{
  long int val;
  Nod *aux, *first;
  first = *l;
	if ((*l) -> next == NULL)
	{
		val = (*l) -> value;
		aux = *l;
		*l = NULL;
		free(aux);
		return val;
	}
	else if ((*l) == NULL)
		return 0;
//verific daca urmatorul element nu este ultimul
	while ((*l) -> next -> next !=NULL)
		(*l) = (*l) -> next;
		
	val = (*l) -> next -> value;
	aux = (*l) -> next;
	(*l) -> next = NULL;
	
//	printf("Am sters un element\n");		
	free(aux); 
 (*l) = first;
 return val;	
}
//returnez valoarea primului element 
long int top(List l) 
{
	long int val;
	val = (l) -> value;
//	printf("Top list afisare \n");
return val;
}
//ultimul element din lista
long int last(List l) 
{
	long int val;
	Nod *first;
	first = l;
	while (l -> next != NULL)
		l = l -> next;
	val = (l) -> value;
//	printf("Top list afisare \n");
return val;
}

//adaog elementul in lista ordonind elementele in ordine descrescatoare incepind de la virf.
void add(List *l,long int val) 
{
	Nod *first,*aux;
	aux =  (Nod *) malloc (sizeof (struct nod) );
	aux -> value = val;
	first = *l;

//e primul element daca lista e vida
	if ((*l) == NULL)
	{
		(*l) = aux;
		(*l) -> next = NULL;
		return;
	}
	else 
//daca e mai mare ca primu element
		if (((*l) -> value < val) && ((*l) -> next != NULL))
		{
			aux -> next = (*l);
			(*l) = aux;
			return;
		}
//daca lista are doar un element se compara cu el si se insereaza
	if ((*l) -> next == NULL)
	{
		if ((*l) -> value < val)
		{
			aux -> next = (*l);
			(*l) = aux;
			return;
		}
		else 
		{
			aux -> next = NULL;
			(*l) -> next = aux;
			return;
		}
	}
//parcurg lista pina cind elementul meu este mai mare decit urmatorul din lista sau nu mai exista urmatorul element
	while (( (*l) -> next) && ((*l) -> next -> value > val) )
		(*l) = (*l) -> next;
//daca exista urm element salvez adresa lui
	if ((*l) -> next)
		aux -> next = (*l) -> next;
	else
		aux -> next = NULL; //daca nu e final
	(*l) -> next = aux; //adaog elentul la lista
	(*l) = first; //ma reintorc la inceputul listei
return;
}


int main()
{
long int i;
List *lists; 
List gutui;
long int high,weight,j;

	FILE *f = fopen("gutui.in","r");
	FILE *f1 = fopen("gutui.out","w");

	fscanf(f,"%ld %ld %ld",&n,&hmax,&u);
	lists = (List *) malloc (n * sizeof(struct nod) );
	gutui = (Nod *) malloc (sizeof(struct nod));
	gutui = NULL;
	rangs = hmax/u +1;
	for (i = 0; i < rangs; i++)
	{
		lists[i] = (Nod *) malloc (sizeof(struct nod) ) ;
		lists[i] = NULL;
	}
	for (i = 0; i < n; i++)
	{
		fscanf(f,"%ld %ld",&high,&weight);	
		if (high < hmax)
		{
			j =(high / u) + ((high % u + u - 1) / u); 
			//printf("%ld %ld \n",j,i);
			add(&lists[j],weight);
		}
	}
	
	for (i=rangs -1; i >=0; i--)
	{
		printf("Ordinul: %ld \n",i);
		if (lists[i] !=NULL)
		{
			printf("am inserat %ld \n",top(lists[i]));
			add(&gutui,delfirst(&lists[i]));
		}
		while (lists[i] !=NULL)
		{
			if (lung(gutui) < (rangs - i + 1))
			{
				add(&gutui,delfirst(&lists[i]));
			}
			else 
				if (top(lists[i]) > last(gutui))
				{
				//	printf("am inserat %ld \n",top(lists[i]));
					//printf("am sters %ld \n",last(gutui));
					dellast(&gutui);
					add(&gutui,delfirst(&lists[i]));
				}
				else lists[i] = NULL;
		}
	
	}
/*	
	for (i=0; i < rangs; i++)
	{
		while (lists[i] !=NULL)
			if (lists[i] != NULL)
			{
				printf("%ld ",top(lists[i]));
				del(&lists[i]);
			}
	printf("alt nivel \n");
	} */
	fprintf(f1,"%ld\n",sum(gutui));
fclose(f);
fclose(f1);
return 0;
}