Cod sursa(job #1428498)

Utilizator stef93Stefan Gilca stef93 Data 4 mai 2015 17:55:45
Problema Heapuri Scor 20
Compilator c Status done
Runda Arhiva educationala Marime 3.19 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// --------------------Utile-----------------------
#define nmax 200003

void * xmalloc(size_t size)
{
	void * ptr = NULL;

	ptr = malloc(size);

	if(ptr == NULL)
	{
		perror("Eroare");
		exit(EXIT_FAILURE);
	}

	return ptr;
}

FILE * open_file(const char * name, const char * opt)
{
	FILE * f = NULL;

	f = fopen(name, opt);

	if(f == NULL)
	{
		perror("Eroare");
		exit(EXIT_FAILURE);
	}
	return f;

}

void swap(int* x, int* y)
{
	int aux;

	aux = *x;
	*x = *y;
	*y = aux;
}
// ----------------------Heap---------------------

struct Heap
{
	int * heap;
	int * poz;
	int * ord;
	int l;
};

typedef struct Heap Heap;

void push(Heap * h,  int x, int p)
{
	int poz = h->l + 1;

	h->heap[++h->l] = x;
	h->poz[p] = h->l;
	h->ord[h->l] = p;

	while(poz / 2 > 0 && h->heap[poz] < h->heap[poz / 2])
	{
		swap(&h->heap[poz], &h->heap[poz/2]);
		swap(&h->ord[poz], &h->ord[poz/2]);
		swap(&h->poz[h->ord[poz/2]], &h->poz[h->ord[poz]]);
		poz /= 2;
	}
}

void pop(Heap *h, int ord)
{
	int poz = h->poz[ord], pozz = h->l;
	char ok = 1;

	swap(&h->heap[poz], &h->heap[pozz]);
	swap(&h->ord[poz], &h->ord[pozz]);
	swap(&h->poz[h->ord[pozz]], &h->poz[h->ord[poz]]);
	h->l--;
	while(ok != 0)
	{
		ok = 0;

		if(2 * poz <= h->l && h->heap[poz] > h->heap[2 * poz])
		{
			ok = 1;
			pozz = 2 * poz;
			//poz = 2 * poz;
		}

		if(2 * poz + 1 <= h->l && h->heap[poz] > h->heap[2 * poz + 1])
		{
			/*if(ok == 1)
			{
				if(h->heap[2 * poz + 1] > h->heap[2*poz])
				{
					pozz = 2 * poz + 1;
				}
				else
				{
					break;
				}
			}*/
			ok = 1;
			pozz = 2 * poz + 1;
			//poz = 2 * poz + 1;
		}
		if(ok == 1){
		swap(&h->heap[poz], &h->heap[pozz]);
		swap(&h->ord[poz], &h->ord[pozz]);
		swap(&h->poz[h->ord[pozz]], &h->poz[h->ord[poz]]);
		poz = pozz;
		}
	}
	//h->l--;
}

// ----------------------Main---------------------
int main()
{
	Heap h;
	int * heap = NULL;
	int * poz = NULL;
	int * cron = NULL;
	int nop, op, x;
	int i, ord = 0, j;

	FILE * in = NULL;
	FILE * out = NULL;

	heap = (int*) xmalloc(sizeof(int) * nmax);
	poz = (int*) xmalloc(sizeof(int) * nmax);
	cron = (int *) xmalloc(sizeof(int) * nmax);

	//memset(poz, 0, nmax * sizeof(int));

	h.ord = cron;
	h.heap = heap;
	h.poz = poz;
	h.l = 0;

	in = open_file("heapuri.in", "r");
	out = open_file("heapuri.out", "w");

	fscanf(in, "%d", &nop);

	for(i = 0 ; i < nop; i++)
	{
		fscanf(in, "%d", &op);

		switch(op)
		{
		case 1:
			ord++;
			fscanf(in , "%d", &x);
			push(&h,x,ord);
			/*printf("q");
			for(j = 1; j <= h.l; j++)
				printf("%d ", h.heap[j]);
			printf("\n");*/
		break;
		case 2:

			fscanf(in , "%d", &x);
			/*printf("%d\n", h.heap[h.poz[x ]]);
			printf("%d\n", h.poz[x]);
			printf("%d\n", h.ord[1]);*/
			pop(&h, x);
			/*printf("d");
			for(j = 1; j <= h.l; j++)
				printf("%d ", h.heap[j]);
			printf("\n");*/
			break;
		case 3:
			fprintf(out, "%d\n", h.heap[1]);
			break;
		default: fprintf(stderr, "Not an option\n"); exit(EXIT_FAILURE);
		}
	}

	free(heap);
	free(poz);
	free(cron);
	fclose(in);
	fclose(out);
	return 0;
}