Pagini recente » Cod sursa (job #3287242) | Cod sursa (job #277668) | Cod sursa (job #1847186) | Cod sursa (job #2706004) | Cod sursa (job #371896)
Cod sursa(job #371896)
# include <fstream>
# include <iostream>
using namespace std;
int h[200002], poz[200002], v[200002], n, m;
void promoveaza (int k)
{
int eh=0;
while (k/2 && !eh)
{
if (v[h[k/2]]<=v[h[k]])
eh=1;
else
{
int a;
a=h[k/2], h[k/2]=h[k], h[k]=a;
poz[h[k]]=k;
poz[h[k/2]]=k/2;
k >>= 1;
}
}
}
void cerne (int k, int n)
{
int eh=0, i;
while (!eh && 2*k<=n)
{
i=k<<1;
if (i+1<=n && v[h[i+1]]<v[h[i]])
i=i+1;
if (v[h[k]]<=v[h[i]])
eh=1;
else
{
int a;
a=h[k], h[k]=h[i], h[i]=a;
poz[h[k]]=k;
poz[h[i]]=i;
k=i;
}
}
}
int main ()
{
ifstream fin ("heapuri.in");
ofstream fout ("heapuri.out");
int nro, o, x;
fin>>nro;
for (;nro;nro--)
{
fin>>o;
if (o==3)
fout<<v[h[1]]<<endl;
else
{
fin>>x;
if (o==1)
{
v[++m]=x;
poz[m]=++n;
h[n]=m;
promoveaza (n);
}
else
{
h[poz[x]]=h[n--];
cerne (poz[x], n);
}
}
}
return 0;
}