Pagini recente » Statistici Mihnea Dragos (idk12344) | Cod sursa (job #1291944) | Cod sursa (job #1006866) | Cod sursa (job #1880003) | Cod sursa (job #282983)
Cod sursa(job #282983)
#include <fstream.h>
ifstream fin("heapuri.in");
int h[100],n;
void sift(int k)
{int son;
do{son=0;
if (2*k<=n)
{son=2*k;
if (son+1<=n && h[son+1]<h[son]) son++;
}
if (h[k]<=h[son]) son=0;
else
{int aux=h[k];
h[k]=h[son];
h[son]=aux;
k=son;
}
}while (son);
}
void percolate(int k)
{int aux;
while (k/2>0 && h[k/2]>h[k])
{aux=h[k/2];
h[k/2]=h[k];
h[k]=aux;
k=k/2;
}
}
void insert(int x)
{n++;
h[n]=x;
percolate(n);
}
void cut(int k)
{h[k]=h[n];
--n;
if (k>1 && h[k]<h[k/2])
percolate(k);
else sift(k);
}
int main()
{int x,m;
fin>>m;
for (int i=1;i<=m;i++)
{fin>>key;
if (key==1)
{fin>>x;
insert(x);}
if (key==2)
{fin>>x; cut(x);}
if (key==3) fout<<h[1]<<'\n';
}
fout.close();
return 0;
}