Pagini recente » Mihnea Andreescu | Cod sursa (job #1247196) | Cod sursa (job #479040) | Cod sursa (job #2691245) | Cod sursa (job #372314)
Cod sursa(job #372314)
#include<fstream>
using namespace std;
int v[10000],h[10000],poz[10000],n,m;
void cerne(int q,int n);
void promoveaza(int q);
int main()
{
int nro,o,x;
ifstream fin("heapuri.in");
fin>>nro;
ofstream fout("heapuri.out");
for(;nro;nro--)
{
fin>>o;
if(0==3)
fout<<v[h[1]];
if(o==1)
{fin>>x;
v[++m]=x;
poz[m]=++n;
h[n]=m;
promoveaza(n);}
else
{
fin>>x;
h[poz[x]]=h[n];
n--;
cerne(poz[x],n);
}
}
}
void promoveaza(int k)
{
int eh=0;
while(k/2!=0 && eh==0)
{
if(v[h[k]]>=v[h[k/2]])
eh=1;
else
{
int aux=h[k];
h[k]=h[k/2];
h[k/2]=aux;
poz[h[k]]=k;
poz[h[k/2]]=k/2;
}
}
}
void cerne(int k,int n)
{
int eh=0;
while(2*k<=n && eh==0)
{
int i=2*k;
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 aux=h[k];
h[k]=h[i];
h[i]=aux;
poz[h[k]]=k;
poz[h[k/2]]=k/2;
k=i;
}
}
}