Cod sursa(job #1814350)

Utilizator pitradaPit-Rada Ionel-Vasile pitrada Data 23 noiembrie 2016 21:25:11
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.34 kb
#include <fstream>

using namespace std;
ifstream fin("heap.in");
ofstream fout("heap.out");

int a[200005],b[200005],n,k;
int heap[200005];
void urcare ( int poz)
{
    int aux;
    if(poz/2>=1 && heap[poz]<heap[poz/2])
    {
        aux=heap[poz/2];
        heap[poz/2]=heap[poz];
        heap[poz]=aux;
        urcare(poz/2);
    }
}
void coborare(int poz)
{
    int aux;
    int r;
    if(2*poz<=n)
    {
        r=2*poz;
        if(2*poz+1<=n && heap[2*poz+1]<heap[r])
        {
            r=2*poz+1;
        }
        if(heap[poz]>heap[r])
        {
            aux=heap[poz];
            heap[poz]=heap[r];
            heap[r]=aux;
            coborare(r);
        }
    }
}
void inserare(int x)
{
    n++;
    heap[n]=x;
    urcare(n);
}

int i,ins,x,N;
int main()
{
    fin>>N;
    for(i=1;i<=N;i++)
    {
        fin>>ins;
        if(ins==1)
        {
            fin>>x;
            k++;
            a[k]=x;
            b[k]=0;
            inserare(x);
        }
        else if(ins==2)
        {
            fin>>x;
            b[x]=1;
        }
        else
        {
            while(b[heap[1]]==1)
            {
                heap[1]=heap[n];
                n--;
                coborare(1);
            }
            fout<<heap[1]<<'\n';
        }
    }
    return 0;
}