Cod sursa(job #1736948)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 2 august 2016 22:32:05
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 kb
#include <bits/stdc++.h>

using namespace std;

void pushHeap(vector<int> &heap, int x)
{
    heap.push_back(x);
    push_heap(heap.begin(), heap.end(), greater<int>());
}

int popHeap(vector<int> &heap)
{
    int x = heap.front();
    pop_heap(heap.begin(), heap.end(), greater<int>());
    heap.pop_back();

    return x;
}

int main()
{
    ifstream in("heapuri.in");
    ofstream out("heapuri.out");

    int N;
    in >> N;

    vector<int> heap;
    vector<int> elems;
    unordered_map<int,int> UMap;

    while (N--)
    {
        int t, x;
        in >> t;

        if (t == 1)
        {
            in >> x;
            elems.push_back(x);
            pushHeap(heap, x);
            UMap[x]++;
        }
        else if (t == 2)
        {
            in >> x;
            int e = elems[x - 1];
            UMap[e]--;
        }
        else
        {
            while (true)
            {
                x = heap.front();

                if (UMap[x] == 0)
                    popHeap(heap);
                else
                    break;
            }

            out << x << "\n";
        }
    }

    return 0;
}