Pagini recente » Cod sursa (job #211162) | Cod sursa (job #2390384) | Cod sursa (job #417511) | Cod sursa (job #138180) | Cod sursa (job #407959)
Cod sursa(job #407959)
#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;
}