Cod sursa(job #3233312)

Utilizator MirceaDonciuLicentaLicenta Mircea Donciu MirceaDonciuLicenta Data 2 iunie 2024 22:59:34
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.46 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#include <unordered_map>

using namespace std;

struct Entry {
    int value;
    int index;

    bool operator>(const Entry& other) const {
        return value > other.value;
    }
};

class Heap {
public:
    void insert(int value, int index) {
        heap.push({value, index});
        index_map[index] = value;
    }

    void remove(int index) {
        if (index_map.find(index) != index_map.end()) {
            index_map.erase(index);
        }
    }

    int getMin() {
        while (!heap.empty() && index_map.find(heap.top().index) == index_map.end()) {
            heap.pop();
        }
        return heap.top().value;
    }

private:
    priority_queue<Entry, vector<Entry>, greater<Entry>> heap;
    unordered_map<int, int> index_map;
};

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

    int N;
    infile >> N;

    Heap heap;
    int operation, x;
    int insert_index = 0;

    for (int i = 0; i < N; ++i) {
        infile >> operation;
        if (operation == 1) {
            infile >> x;
            heap.insert(x, insert_index++);
        } else if (operation == 2) {
            infile >> x;
            heap.remove(x - 1);
        } else if (operation == 3) {
            outfile << heap.getMin() << endl;
        }
    }

    infile.close();
    outfile.close();

    return 0;
}