Cod sursa(job #2491394)

Utilizator StanCatalinStanCatalin StanCatalin Data 12 noiembrie 2019 15:37:09
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.59 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int dim = 200005;

int heap[dim],h_size,n,v[dim],de_sters[dim],poz[dim],cnt = 0;

void Urca(int poz)
{
    if (poz == 1) return ;

    if (v[heap[poz]] < v[heap[poz/2]])
    {
        swap(heap[poz] , heap[poz/2]);
        Urca(poz/2);
    }
    else return;
}

void Adauga(int val)
{
    v[cnt] = val;
    heap[++h_size] = cnt;
    Urca(h_size);
}

void Coboara(int poz)
{
    if (2*poz + 1 <= h_size)
    {
        int pozmin = 2*poz;
        if (v[heap[2*poz+1]] < v[heap[2*poz]])
        {
            pozmin = 2*poz+1;
        }
        if (v[heap[poz]] > v[heap[pozmin]])
        {
            swap(heap[poz] , heap[pozmin]);
            Coboara(pozmin);
        }
    }
    else if (2*poz == h_size)
    {
        if (v[heap[poz]] > v[heap[2*poz]])
        {
            swap(heap[poz] , heap[2*poz]);
            Coboara(2*poz);
        }
    }
    return ;
}

int main()
{
    in >> n;
    int x,a;
    for (int i=1; i<=n; i++)
    {
        in >> x;
        if (x == 1)
        {
            in >> a;
            cnt++;
            Adauga(a);
        }
        if (x == 2)
        {
            in >> a;
            de_sters[a] = 1;
        }
        if (x == 3)
        {
            while (de_sters[heap[1]])
            {
                swap(heap[1] , heap[h_size]);
                h_size--;
                Coboara(1);
            }
            out << v[heap[1]] << "\n";
        }
    }
    return 0;
}