Cod sursa(job #434260)

Utilizator adrian.lunguLungu Adrian adrian.lungu Data 5 aprilie 2010 15:21:49
Problema Gutui Scor 80
Compilator c Status done
Runda teme_upb Marime 3.32 kb
#include <stdio.h>
#include <stdlib.h>

#define true 1
#define false 0

typedef struct Node{
      int h,w;
      struct Node *next;
} node;

typedef node* linkedList;



node* newNode (int h, int w, node *nextNode){
      node *aNode = (node*)malloc(sizeof(node));
      aNode -> h = h;
      aNode -> w = w;
      aNode -> next = nextNode;
      return aNode;
}

void setNext(node *aNode, node* nextNode){
      aNode -> next = nextNode;
}

int listInsertAsc(linkedList aList, int maxEl, node *aNode) {
      node * tempNode = aList;
      while (tempNode -> next != NULL && tempNode -> next -> w < aNode -> w) {
	    tempNode = tempNode -> next;
      }
      setNext(aNode,tempNode->next);
      setNext(tempNode,aNode);
      return true;
}

int listInsertDesc(linkedList aList, int maxEl, node *aNode) {
      node * tempNode = aList;
      int listCount = 0;
      while (tempNode -> next != NULL && tempNode -> next -> w > aNode -> w && listCount < maxEl) {
	    tempNode = tempNode -> next;
	    listCount ++;
      }
      if (listCount == maxEl)
	    return false;
      setNext(aNode,tempNode->next);
      setNext(tempNode,aNode);
      return true;
}

int isEmpty(linkedList aList){
      if (aList -> next == NULL)
	    return true;
      return false;
}

node *getNextNode(linkedList aList){
      node * tempNode = aList -> next;
      aList -> next = aList -> next -> next;
      return tempNode;
}

void printList(linkedList aList) {
      node * tempNode = aList -> next;
      while (tempNode != NULL){
	    printf("%d %d\n",tempNode -> h, tempNode -> w);
	    tempNode = tempNode ->next;
      }
}

int main() {
      
      FILE *fin, *fout;
      int N, H, U;
      fin = fopen("gutui.in","r");
      fout = fopen("gutui.out","w");
      fscanf(fin,"%d %d %d", &N, &H, &U);
      int i;
      
      // Make list of lists
      int noOfLists = H/U;
      if (H % U) noOfLists++;
      linkedList *lists = (linkedList*) malloc (noOfLists * sizeof(linkedList));
      for (i = 0 ; i < noOfLists ; i++) {
	    lists[i] = (linkedList) malloc(sizeof(node));
      }
      
      // Read elements and fill the lists
      for (i = 0 ; i < N ; i++) {
	    int h, w, listNo;
	    fscanf(fin,"%d %d",&h, &w);
	    listNo = (H - h)/U;
	    //printf("%d %d %d\n",h,w,listNo);
	    listInsertDesc(lists[listNo],i+2,newNode(h,w,NULL));
      }
/*
      // print lists
      for (i = 0 ; i < noOfLists ; i++) {
	    printf("lista %d:\n",i);
	    //if (lists[i] -> next != NULL)
		  printList(lists[i]);
      }
*/
      linkedList selected = (linkedList) malloc (sizeof(node));
      
      int listCount = 0;
      for (i = 0 ; i < noOfLists ; i++) {
	    while (isEmpty(lists[i]) == false) {
		  listInsertAsc(selected,i + 1,getNextNode(lists[i]));
		  listCount ++;
		  //printf("Lista dupa inserare: \n");
		  //printList(selected);
		  if (listCount > i + 1) {
			getNextNode(selected);
		//	printf("pas = %d, dim lista = %d, Lista dupa stergere: \n",i + 1,listCount);
		//	printList(selected);
			listCount --;
		  }
	    }
      }
      
      //printf("lista finala:\n");
      unsigned long long suma = 0;
      while (isEmpty(selected) == false) {
	    node *tempNode = getNextNode(selected);
	    suma += tempNode -> w;
	//    printf("%d %d\n",tempNode -> h,tempNode -> w);
      }
      fprintf(fout,"%lld\n",suma);
      fclose(fin);
      fclose(fout);
      return 0;
}