Cod sursa(job #236371)

Utilizator gabitzish1Gabriel Bitis gabitzish1 Data 27 decembrie 2008 12:20:16
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.02 kb
#include <stdio.h>

#define MAX 200005

int N, K, poz[MAX], heap[MAX];

void swap(int x, int y)
{
	int a;
	a = heap[x]; heap[x] = heap[y]; heap[y] = a;
	a = poz[x]; poz[x] = poz[y]; poz[y] = a;

}

void urc(int k)
{
	if (k > 1)
		if (heap[k] < heap[k / 2])
			swap(k, k / 2);			
}

void cobor(int k)
{
	int min, st, dr;
	st = 2 * k; dr = st + 1;

	min = k;
	if (st <= K) {if (heap[k] > heap[st]) min = st;}
	if (dr <= K) {if (heap[min] > heap[dr]) min = dr;}

	swap(k, min);
}

void sterg(int k)
{
	swap(k, K);
	K--;
	cobor(k);
}




int main()
{
	freopen("heapuri.in","r",stdin);
	freopen("heapuri.out","w",stdout);

	int i, op, x;

	scanf("%d", &N);
	for (i = 1; i <= N; i++)
	{
		scanf("%d",&op);
		switch (op)
		{
		case 1:
			{
				scanf("%d", &x);
				heap[++K] = x; poz[i] = K;
				urc(K);
				break;
			}
		case 2:
			{
				scanf("%d", &x);
				sterg(poz[x]);
				break;
			}
		case 3: 
			{
				printf("%d\n",heap[1]);
				break;
			}
		}
	}
	return 0;
}