Pagini recente » Cod sursa (job #3197720) | Cod sursa (job #903305) | Cod sursa (job #1871076) | Cod sursa (job #44926) | Cod sursa (job #496858)
Cod sursa(job #496858)
#include<stdio.h>
#include<algorithm>
using namespace std;
FILE*f=fopen("heapuri.in","r");
FILE*g=fopen("heapuri.out","w");
int i,NN,heap[200100],n,caz,c,p,x,pos[200100],elemente,A[200100],nr,u;
void add_heap(int x){
c = x; p = x/2;
while( p != 0 && A[heap[p]] > A[heap[c]] ){
swap(heap[p],heap[c]);
pos[heap[p]] = p;
pos[heap[c]] = c;
c = p;
p = p / 2;
}
}
void coboara(int x)
{
int y = 0;
while (x != y)
{
y = x;
if (y*2<=elemente && A[heap[x]]>A[heap[y*2]]) x = y*2;
if (y*2+1<=elemente && A[heap[x]]>A[heap[y*2+1]]) x = y*2+1;
swap(heap[x],heap[y]);
pos[heap[x]] = x;
pos[heap[y]] = y;
}
}
int main () {
fscanf(f,"%d",&NN);
for ( i = 1 ; i <= NN ; ++i ){
fscanf(f,"%d",&caz);
switch(caz){
case 1 :{
fscanf(f,"%d",&x);
elemente++; nr++;
A[nr] = x;
heap[elemente] = nr;
pos[nr] = elemente;
add_heap(elemente);
break;
}
case 2:{
fscanf(f,"%d",&x);
A[x] = -1;
add_heap(pos[x]);
pos[heap[1]] = 0;
heap[1] = heap[elemente--];
pos[heap[1]] = 1;
if( elemente > 1 )
coboara(1);
break;
}
case 3:{
fprintf(g,"%d\n",A[heap[1]]);
break;
}
}
}
fclose(f);
fclose(g);
return 0;
}