Cod sursa(job #407955)

Utilizator lamez0rBogdan Bondor lamez0r Data 2 martie 2010 19:09:47
Problema Heapuri Scor 0
Compilator c Status done
Runda Arhiva educationala Marime 1.21 kb
#include<stdio.h>
#include<algo.h>

int heap[200001],n,v[200001],poz;
FILE *f,*g;

void insert (int k)
{
	int j,aux;
	j=poz;
	while (j>1 && heap[j/2]>heap[j])
	{
		aux=heap[j/2];
		heap[j/2]=heap[j];
		heap[j]=aux;
		j/=2;
	}
}
	
void sterge (int k)
{
	int i,j,aux;
	for (i=1;i<=poz;++i)
		if (heap[i]==k)
		{
			heap[i]=heap[poz];
			poz--;
			j=i;
		}
	if (heap[j/2]>heap[j])
		while (j>1 && heap[j/2]>k)
		{
			aux=heap[j/2];
			heap[j/2]=heap[j];
			heap[j]=aux;
			j/=2;
		}
	else
		while(j<=poz/2 && (heap[j*2]<heap[j] || heap[j*2+1]<heap[j]) )
			if(heap[j*2]<heap[j*2+1])
			{
				aux=heap[j*2];
				heap[j*2]=heap[j];
				heap[j]=aux;
				j=j*2;
			}
			else
			{
				aux=heap[j*2+1];
				heap[j*2+1]=heap[j];
				heap[j]=aux;
				j=j*2+1;
			}
}

void read ()
{
	int i,op,k;
	f=fopen("heapuri.in","r");
	g=fopen("heapuri.out","w");
	fscanf(f,"%d",&n);
	while (!feof(f))
	{
		fscanf (f,"%d",&op);
		if (op==1)
		{
			++poz;
			fscanf(f,"%d",&heap[poz]);
			v[poz]=heap[poz];
			insert(heap[poz]);
		}
		if (op==2)
		{
			fscanf(f,"%d",&k);
			sterge (v[k]);
		}
		if (op==3)
			fprintf(g,"%d\n",heap[1]);
	}
}

int main ()
{
	read();
	return 0;
}