Cod sursa(job #1815907)

Utilizator pitradaPit-Rada Ionel-Vasile pitrada Data 25 noiembrie 2016 21:51:07
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.29 kb
#include <iostream>
#include <fstream>

using namespace std;

int a[1000000], n, b[1000000], h[1000000];

void coborare (int h[], int p, int n)
{
    int aux, r;
    while(2*p<=n)
    {
        r=2*p;
        if(r+1<=n&&a[h[r+1]]<a[h[r]])
            r=r+1;
        if(a[h[r]]<a[h[p]])
        {
            aux=h[r]; h[r]=h[p]; h[p]=aux;
            p=r;
        }
        else break;
    }
}

void urcare (int h[], int p, int n)
{
    int aux;
    while(p/2>=1&&a[h[p/2]]>a[h[p]])
    {
        aux=h[p/2];
        h[p/2]=h[p];
        h[p]=aux;
        p=p/2;
    }
}

int main()
{   ifstream fin("heapuri.in");
    ofstream fout("heapuri.out");
    int i, x, k=0, m=0, comanda;
    fin>>n;
    for(i=1;i<=n;i++)
    {
        fin>>comanda;
        if(comanda==1)
        {
            fin>>x;
            k++; a[k]=x; b[k]=0;
            m++; h[m]=k; urcare(h,m,m);
        }
        if(comanda==2)
        {
            fin>>x;
            b[x]=1;
        }
        if(comanda==3)
        {
            while(b[h[1]]==1)
            {///while(b[h[m]]==1) m--;
             h[1]=h[m];
             m--;
             coborare(h,1,m);
            }
            fout<<a[h[1]]<<"\n";
        }
    }
    fin.close();
    fout.close();
    return 0;
}