Cod sursa(job #794497)

Utilizator vladstoickvladstoick vladstoick Data 6 octombrie 2012 14:01:27
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.82 kb
#include<fstream>
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
const int NMAX=200001;
int heap[NMAX];
int poz[NMAX];
int elementdeIntrodus[NMAX];
int lungHeap , nrOperatii , tipOp , elementdeSters;
void schimba(int i,int j)
{
    int aux;
    aux=poz[heap[i]];
    poz[heap[i]]=poz[heap[j]];
    poz[heap[j]]=aux;
    aux=heap[i];
    heap[i]=heap[j];
    heap[j]=aux;
}
void urca(int pozitie)
{
    if(pozitie==1)
        return;
    while(elementdeIntrodus[heap[pozitie]]<elementdeIntrodus[heap[pozitie/2]])
    {
        schimba(pozitie,pozitie/2);
        pozitie=pozitie/2;
    }
}
void coboara(int pozitie)
{
    int fs=2*pozitie;
    int fd=2*pozitie+1;
    int bun=pozitie;
    if(fs<=lungHeap && elementdeIntrodus[heap[pozitie]]>elementdeIntrodus[heap[fs]])
        bun=fs;
    if(fd<=lungHeap && elementdeIntrodus[heap[pozitie]]>elementdeIntrodus[heap[fd]])
        bun=fd;
    if(bun!=pozitie)
    {
        schimba(pozitie,bun);
        coboara(pozitie);
    }
}
void sterge(int pozitie)
{
    int aux=poz[pozitie];
    schimba(poz[pozitie],lungHeap);
    heap[lungHeap]=0;
    lungHeap--;
    if(elementdeIntrodus[heap[aux]]<elementdeIntrodus[heap[aux/2]])
        urca(aux);
    else
        coboara(aux);
}
int main()
{
    in>>nrOperatii;
    int nrCitite=0;
    for(int i=1;i<=nrOperatii;i++)
    {
        in>>tipOp;
        if(tipOp==1)
        {
            in>>elementdeIntrodus[++nrCitite];
            heap[++lungHeap]=lungHeap;
            poz[lungHeap]=lungHeap;
            urca(lungHeap);
        }
        if(tipOp==2)
        {
            in>>elementdeSters;
            sterge(elementdeSters);
        }
        if(tipOp==3)
        {
            out<<elementdeIntrodus[heap[1]]<<"\n";
        }
    }
    return 0;
}