Mai intai trebuie sa te autentifici.
Cod sursa(job #357699)
Utilizator | Data | 20 octombrie 2009 12:45:34 | |
---|---|---|---|
Problema | Heapuri | Scor | 10 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 1.23 kb |
#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 (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;
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]=nr;
urca(nr);
}
else if (temp==2)
{
scanf("%ld",&temp);
na=nr+1;
while(na && h[--na]!=temp);
v[na]=v[nr];
h[na]=h[nr];
--nr;
urca(na);
}
else
{
printf("%ld\n",v[1]);
}
}
return 0;
}