Pagini recente » Cod sursa (job #2855666) | Cod sursa (job #883143) | Cod sursa (job #966199) | Cod sursa (job #2987721) | Cod sursa (job #255560)
Cod sursa(job #255560)
#include<stdio.h>
#define maxn 200001
FILE *f=fopen("heapuri.in","r"), *g=fopen("heapuri.out","w");
int n,i,h[maxn],pos[maxn],a[maxn],x,cod,L,nr;
void push(int x)
{
int aux;
while(x/2 && a[h[x]]<a[h[x/2]])
{
aux=h[x];
h[x]=h[x/2];
h[x/2]=aux;
pos[h[x]]=x;
pos[h[x/2]]=x/2;
x=x/2;
}
}
void pop(int x)
{
int y=0,aux;
while(x!=y)
{
y=x;
if(y*2<=L && a[h[x]]>a[h[y*2]]) x=y*2;
if(y*2+1<=L && a[h[x]]>a[h[y*2+1]]) x=y*2+1;
aux=h[x];
h[x]=h[y];
h[y]=aux;
pos[h[x]]=x;
pos[h[y]]=y;
}
}
int main()
{
fscanf(f,"%d",&n);
for(i=1;i<=n;i++)
{
fscanf(f,"%d",&cod);
if(cod==3)
{
fprintf(g,"%d\n",a[h[1]]);
}
else if(cod==2)
{
fscanf(f,"%d",&x);
a[x]=-1;
push(pos[x]);
pos[h[1]]=0;
h[1]=h[L--];
pos[h[1]]=1;
if(L>1) pop(1);
}
else if(cod==1)
{
fscanf(f,"%d",&x);
L++;
nr++;
a[nr]=x;
pos[nr]=L;
h[L]=nr;
push(L);
}
}
fclose(f);
fclose(g);
return 0;
}