Cod sursa(job #1814357)

Utilizator pitradaPit-Rada Ionel-Vasile pitrada Data 23 noiembrie 2016 21:33:03
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.32 kb
#include <fstream>

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

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

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