Pagini recente » Cod sursa (job #2945168) | Profil vladbutnaru | Cod sursa (job #1732732) | Cod sursa (job #364033) | Cod sursa (job #357798)
Cod sursa(job #357798)
#include<stdio.h>
long n,nr,h[2010],v[2010];
void coboara(long temp)
{
long aux;
while(temp<=nr/2)
{
if (v[temp]>v[temp*2] || v[temp]>v[temp*2+1] )
{
if(temp*2==nr)
{
if (v[temp*2]<v[temp])
{
aux=v[temp*2];
v[temp*2]=v[temp];
v[temp]=aux;
aux=h[temp*2];
h[temp*2]=h[temp];
h[temp]=aux;
temp=temp*2;
}
else break;
}
else if(v[temp*2+1]<=v[temp*2])
{
aux=v[temp*2+1];
v[temp*2+1]=v[temp];
v[temp]=aux;
aux=h[temp*2+1];
h[temp*2+1]=h[temp];
h[temp]=aux;
temp=temp*2+1;
}
else
if (v[temp*2]<=v[temp*2+1])
{
aux=v[temp*2];
v[temp*2]=v[temp];
v[temp]=aux;
aux=h[temp*2];
h[temp*2]=h[temp];
h[temp]=aux;
temp=temp*2;
}
else
{
aux=v[temp*2+1];
v[temp*2+1]=v[temp];
v[temp]=aux;
aux=h[temp*2+1];
h[temp*2+1]=h[temp];
h[temp]=aux;
temp=temp*2+1;
}
}
else break;
}
}
void urca(long temp)
{
long aux;
while(temp>=2&& nr>2)
{
if (v[temp]<v[temp/2])
{
aux=v[temp];
v[temp]=v[temp/2];
v[temp/2]=aux;
aux=h[temp];
h[temp]=h[temp/2];
h[temp/2]=aux;
temp=temp/2;
}
else break;
}
coboara(temp);
}
int main()
{
long temp,i,na,x=0;
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
scanf("%ld",&n);
for (i=1;i<=n;++i)
{
scanf("%ld",&temp);
if (temp==1)
{
scanf("%ld",&temp);
v[++nr]=temp;
h[nr]=++x;
urca(nr);
}
else if (temp==2)
{
scanf("%ld",&temp);
na=nr+1;
while(h[--na]!=temp);
v[na]=v[nr];
h[na]=h[nr];
--nr;
urca(na);
}
else
{
printf("%ld\n",v[1]);
}
}
return 0;
}