Cod sursa(job #758392)

Utilizator taigi100Cazacu Robert taigi100 Data 15 iunie 2012 15:55:43
Problema Heapuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.39 kb
#include<stdio.h>
using namespace std;
#define N 200005

int heap[N],poze[N],val[N],el;
  FILE *f=fopen("heapuri.in","r");
    FILE *g=fopen("heapuri.out","w");
void swap(int x,int y)
{
	int aux;
	aux=heap[x];
	heap[x]=heap[y];
	heap[y]=aux;
	aux=poze[x];
	poze[x]=poze[y];
	poze[y]=aux;
}
void father(int poz)
{
	while(heap[poz/2]>heap[poz]&&poz>=2)
	{	swap(poz/2,poz);
	poz/=2;
	}
}
void add(int val)
{
	el++;
     heap[el]=val;
	 poze[el]=el;
	 father(el);
}

int mind(int a,int b)
{
	return heap[a]>heap[b] ? b:a;
}
void son(int poz)
{
	int mda;
	while(heap[poz]>(mda=mind(poz*2,poz*2+1)) && poz*2+1<el)
	{
		swap(poz,mda);
	    poz=mda;
	}
	if(heap[poz*2]<heap[poz])
    {
		swap(poz,poz*2);
	}
	
}
void del(int cronos)
{

	for(int i=1;i<=el;i++)
	{
		if(poze[i]==cronos)
		{
		cronos=i;
		i=el+2;
		}
	}
	
	
	heap[cronos]=heap[el];
	poze[cronos]=poze[el];
	el--;
	son(cronos);
	
}
int main()
{
    int n;
    fscanf(f,"%d",&n);
    int cod,mata;
  
    for(int i=1;i<=n;i++)
    {
            fscanf(f,"%d",&cod);
            if(cod==3)
            {
                      fprintf(g,"%d\n",heap[1]);
            }
            if(cod==1)
            {
				fscanf(f,"%d",&mata);
            add(mata);
			}
            if(cod==2)
            {
				fscanf(f,"%d",&mata);
                del(mata);
            }
    }
    
}