Pagini recente » Cod sursa (job #1771720) | Cod sursa (job #2060028) | Cod sursa (job #853479) | Cod sursa (job #1995549) | Cod sursa (job #2275663)
#include <stdio.h>
#include <stdlib.h>
int values[200001],heap[200001],poz_heap[200001];
void upheap(int x){
int aux;
while(x/2 && values[heap[x]]<values[heap[x/2]]){
aux=heap[x];
heap[x]=heap[x/2];
heap[x/2]=aux;
poz_heap[heap[x]]=x;
poz_heap[heap[x/2]]=x/2;
x/=2;
}
}
void downheap(int x,int nr_heap){
int aux,y=0;
while(x!=y){
y=x;
if(y*2<=nr_heap && values[heap[x]]>values[heap[y*2]]) x=y*2;
if(y*2+1<=nr_heap && values[heap[x]]>values[heap[y*2+1]]) x=y*2+1;
aux=heap[x];
heap[x]=heap[y];
heap[y]=aux;
poz_heap[heap[x]]=x;
poz_heap[heap[y]]=y;
}
}
int main()
{
FILE*fin,*fout;
fin = fopen("heapuri.in" ,"r");
fout = fopen("heapuri.out" ,"w");
int n;
fscanf(fin, "%d" ,&n);
int i,operation,nr_val=0,nr_heap=0,x;
for(i=0;i<n;i++){
fscanf(fin, "%d" ,&operation);
if(operation==1){
fscanf(fin, "%d" ,&x);
nr_val++; nr_heap++;
values[nr_val]=x;
heap[nr_heap]=nr_val;
poz_heap[nr_val]=nr_heap;
upheap(nr_heap);
}
else if(operation==2){
fscanf(fin, "%d" ,&x);
values[x]=-1;
upheap(poz_heap[x]);
poz_heap[heap[1]]=0;
heap[1]=heap[nr_heap--];
poz_heap[heap[1]]=1;
if(nr_heap>1) downheap(1,nr_heap);
}
else{
fprintf(fout, "%d\n" ,values[heap[1]]);
}
}
return 0;
}