Pagini recente » Cod sursa (job #1580088) | Cod sursa (job #1470466) | Cod sursa (job #2692883) | Cod sursa (job #3209225) | Cod sursa (job #1514453)
#include <iostream>
#include <fstream>
using namespace std;
int nh,nr,v[100],h[100],pos[100],k; //p[i] ==> i= ordinea citirii; p[i]=poz actuala in vect h
void schimb(int p, int q)
{
int aux=h[p];
h[p]=h[q];
h[q]=aux;
pos[h[p]]=p;
pos[h[q]]=q;
}
void urca(int p)
{
while(p>1 && v[h[p]]<v[h[p/2]])
{
schimb(p,p/2);
p/=2;
}
}
void coboara(int p)
{
int fs=2*p,fd=2*p+1, bun=p;
if(fs<=nh && v[h[fs]]<v[h[bun]])
bun=fs;
if(fd<=nh && v[h[fd]]<v[h[bun]])
bun=fd;
if(bun!=p)
{
schimb(bun,p);
coboara(bun);
}
}
int main()
{
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int i,p,m,x,cod;
fin>>m;
for(i=1;i<=m;i++)
{
fin>>cod;
if(cod==1)
{
fin>>v[++nr];
h[++nh]=nr;
pos[nr]=nh;
urca(nh);
}
if(cod==2)
{
fin>>x;
p=pos[x];
schimb(p,nh--);
urca(p);
coboara(p);
}
if(cod==3)
fout<<v[h[1]]<<endl;
}
return 0;
}