Pagini recente » Cod sursa (job #166992) | Cod sursa (job #36010) | Cod sursa (job #219765) | Cod sursa (job #2600866) | Cod sursa (job #805629)
Cod sursa(job #805629)
#include <fstream>
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
int h[100], v[100], poz[100],i,n,x,y,nh,k;
void schimba(int x, int y)
{
int aux;
aux=0;
aux=h[x]; h[x]=h[y]; h[y]=aux;
poz[h[x]]=x;
poz[h[y]]=y;
}
void urc(int p)
{
if (p==1||v[h[p/2]]<=v[h[p]]) return;
schimba(p/2,p);
urc(p/2);
}
void cobor(int p)
{
int fs=p*2,fd=p*2+1,max1=p,aux1;
if(fs<=nh && v[h[fs]]<v[h[max1]]) max1=fs;
if(fd<=nh && v[h[fd]]<v[h[max1]]) max1=fd;
if (max1!=p)
{
schimba(p,max1);
cobor(max1);
}
}
int main()
{
in>>n; nh=0; k=0;
for (i=1;i<=n;i++)
{
in>>x;
if (x==1)
{
in>>v[++k];
h[++nh]=k;
poz[k]=nh;
urc(nh);
}
if(x==2)
{
in>>y;
h[poz[y]]=h[nh];
nh--;
cobor(poz[y]);
urc(poz[y]);
}
if(x==3) out<<v[h[1]]<<"\n";
}
return 0;
}