Cod sursa(job #2741587)

Utilizator icnsrNicula Ionut icnsr Data 16 aprilie 2021 16:42:36
Problema Heapuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.84 kb
#include <fstream>
std::ifstream fin("heapuri.in");
std::ofstream fout("heapuri.out");

#include <vector>
#include <algorithm>
#include <unordered_set>

class LazyHeap
{
public:
        using value_t = std::pair<int, unsigned>;

        static constexpr auto compare = [](const value_t& x1, const value_t& x2)
        {
                return x1.first > x2.first;
        };

        void push(const int x)
        {
                elements.push_back({x, count});
                std::push_heap(elements.begin(), elements.end(), compare);
                ++count;
        }

        void erase(const unsigned k)
        {
                deleted.insert(k);
        }

        int top()
        {
                while(deleted.find(elements.front().second) != deleted.cend())
                {
                        std::pop_heap(elements.begin(), elements.end(), compare);
                        elements.pop_back();
                }

                return elements.front().first;
        }

private:
        std::vector<value_t> elements;
        std::unordered_set<unsigned> deleted;
        unsigned count = 1;
};

int main()
{
        unsigned N;
        fin >> N;

        LazyHeap heap;
        for(unsigned i = 0; i < N; ++i)
        {
                int op;
                fin >> op;

                int value;

                if(op != 3)
                {
                        fin >> value;
                }

                switch(op)
                {
                        case 1:
                                heap.push(value);
                                break;
                        case 2:
                                heap.erase(value);
                                break;
                        case 3:
                                fout << heap.top() << '\n';
                                break;
                }
        }
}