Pagini recente » Cod sursa (job #873704) | Cod sursa (job #2048793) | Cod sursa (job #2122156) | Cod sursa (job #658419) | Cod sursa (job #3233312)
#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;
}