Cod sursa(job #2725665)

Utilizator preda.andreiPreda Andrei preda.andrei Data 19 martie 2021 14:40:52
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.16 kb
#include <fstream>
#include <queue>
#include <unordered_set>

using namespace std;

template <typename T>
using MinHeap = priority_queue<T, vector<T>, greater<T>>;

class MinSet {
public:
    void Insert(int value);

    void Remove(int kth);

    int Min();

private:
    using Node = pair<int, int>;

    MinHeap<Node> nodes_;
    unordered_set<int> removed_;
    int inserted_ = 0;
};

void MinSet::Insert(int value) {
    nodes_.push({value, inserted_});
    inserted_ += 1;
}

void MinSet::Remove(int kth) {
    removed_.insert(kth);
}

int MinSet::Min() {
    while (removed_.count(nodes_.top().second)) {
        nodes_.pop();
    }
    return nodes_.top().first;
}

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

    int n;
    fin >> n;

    MinSet set;
    for (auto i = 0; i < n; i += 1) {
        int type;
        fin >> type;

        if (type == 1) {
            int value;
            fin >> value;
            set.Insert(value);
        } else if (type == 2) {
            int kth;
            fin >> kth;
            set.Remove(kth - 1);
        } else if (type == 3) {
            fout << set.Min() << "\n";
        }
    }
    return 0;
}