Cod sursa(job #1435469)

Utilizator TPotecTiberiu Potec TPotec Data 13 mai 2015 14:32:00
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include <fstream>

using namespace std;

ifstream in("date.in");
ofstream out("date.out");

int n,nh,h[1001],v[1001],poz[1001];

void schimb(int p, int q)
{
    int aux=h[p];
    h[p]=h[q];
    h[q]=aux;
    poz[h[p]]=p;
    poz[h[q]]=q;
}

void urca(int p)
{
    while(p>1 && v[h[p]]<v[h[p/2]])
    {
        schimb(p,p/2);
        p/=2;
    }
}

void adaugaH(int x)
{
    h[++nh]=x;
    poz[x]=nh;
    urca(nh);
}

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(p,bun);
        coboara(bun);
    }
}

void stergeH(int p)
{
    schimb(p,nh--);
    urca(p);
    coboara(p);
}

int main()
{
    int i,x,op;
    in>>n;
    int nr=0;
    for(i=1; i<=n; i++)
    {
        in>>op;
        if(op==3)
            out<<v[h[1]];
        if(op==1)
        {
            nr++;
            in>>v[nr];
            adaugaH(nr);
        }
        if(op==2)
        {
            in>>x;
            stergeH(poz[x]);
            out<<'\n';
        }
    }
    return 0;
}