Cod sursa(job #2140613)

Utilizator fylot3Bogdan Filote fylot3 Data 23 februarie 2018 18:14:38
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 3.92 kb
# include <stdio.h>
# include <stdlib.h>
# include <vector>

#define MAX_N	200000
#define MAX_VAL	100000000

int indecies[MAX_VAL];

//int indicies[MAX_VAL];
//int negIndicies[MAX_VAL];

typedef struct MinHeap_s {
	int elements[MAX_N];
	int capacity;
	int size;
	//int *indecies;
} MinHeap;

void initHeap(MinHeap *heap, int cap) {
	//heap = (MinHeap *)malloc(sizeof(MinHeap *));
	heap->capacity = cap;
	heap->size = 0;
	//heap->elements = (int *)malloc(cap * sizeof(int));
	//heap->indecies = (int *)malloc(MAX_VAL * sizeof(int));
}

void swap(int *a, int *b) {
	int temp = *a;
	*a = *b;
	*b = temp;
}

int parentHeap(int child) {
	return (child - 1) / 2;
}

int leftChild(int parent) {
	return 2 * parent + 1;
}

int rightChild(int parent) {
	return 2 * parent + 2;
}

void heapifyUp(MinHeap *heap, int ind) {
	while (ind != 0 && heap->elements[parentHeap(ind)] > heap->elements[ind]) {
		swap(&heap->elements[parentHeap(ind)], &heap->elements[ind]);
		swap(&indecies[heap->elements[parentHeap(ind)]], &indecies[heap->elements[ind]]);
		ind = parentHeap(ind);
	}
}

void insertKey(MinHeap *heap, int key) {

	if (heap->size == heap->capacity) {
		return;
	}

	heap->size++;
	int ind = heap->size - 1;
	heap->elements[ind] = key;
	indecies[key] = ind + 1;

	heapifyUp(heap, ind);
}

void heapifyDown(MinHeap *heap, int ind) {
	int left = leftChild(ind);
	int right = rightChild(ind);
	int min = ind;

	if (left < heap->size && heap->elements[left] < heap->elements[ind])
		min = left;
	if (right < heap->size && heap->elements[right] < heap->elements[min])
		min = right;
	if (min != ind) {
		swap(&heap->elements[ind], &heap->elements[min]);
		swap(&indecies[heap->elements[ind]], &indecies[heap->elements[min]]);
		heapifyDown(heap, min);
	}
}

int peekMin(MinHeap *heap) {
	if (heap->size > 0)
		return heap->elements[0];
	return -1;
}

void pop(MinHeap *heap) {

	if (heap->size == 0) {
		return;
	}

	if (heap->size == 1) {
		heap->size--;
		return;
	}

	heap->elements[0] = heap->elements[heap->size - 1];
	indecies[heap->elements[heap->size - 1]] = 1;
	//heap->elements[heap->size - 1] = INT_MAX;
	heap->size--;
	heapifyDown(heap, 0);
}

void decreaseKey(MinHeap *heap, int ind, int newValue) {
	heap->elements[ind] = newValue;
	heapifyUp(heap, ind);
}

void deleteKey(MinHeap *heap, int ind) {
	decreaseKey(heap, ind, INT_MIN);
	pop(heap);
}

int searchVal(MinHeap *heap, int value, int ind) {

	if (ind >= heap->size)
		return -1;

	if (heap->elements[ind] == value)
		return ind;

	int left = leftChild(ind);
	int right = rightChild(ind);
	int foundInd = -1;

	if (left < heap->size && value >= heap->elements[left])
		foundInd = searchVal(heap, value, left);
	if (foundInd < 0) {
		if (right < heap->size && value >= heap->elements[right])
			foundInd = searchVal(heap, value, right);
		else
			foundInd = -1;
	}

	return foundInd;
}

/*void setIndex(int x, int ind) {
	if (x >= 0)
		indicies[x] = ind;
	else
		negIndicies[x + MAX_VAL] = ind;
}

int getIndex(int x) {
	if (x >= 0)
		return indicies[x];
	return negIndicies[x + MAX_VAL];
}*/

int main(void) {
	MinHeap heap;
	initHeap(&heap, MAX_N);

	//indicies = (int *)malloc(MAX_VAL * sizeof(int));
	//negIndicies = (int *)malloc(MAX_VAL * sizeof(int));

	int N, op, x, min;
	FILE *fin = fopen("heapuri.in", "r");
	FILE *fout = fopen("heapuri.out", "w");
	fscanf(fin, "%d", &N);

	std::vector<int> v;

	for (int i = 0; i < N; i++) {
		fscanf(fin, "%d", &op);
		//printf("Do op %d\n", op);
		if (op == 3) {
			min = peekMin(&heap);
			fprintf(fout, "%d\n", min);
			continue;
		}

		fscanf(fin, "%d", &x);
		if (op == 1) {
			//setIndex(x, v.size());
			v.push_back(x);
			insertKey(&heap, x);
		}
		else {
			//int ind = getIndex(v[x - 1]);
			int ind = indecies[v[x - 1]];
			deleteKey(&heap, ind - 1);
		}
	}

	fclose(fin);
	fclose(fout);

	return 0;
}