Pagini recente » Cod sursa (job #2185818) | Cod sursa (job #3140215) | Cod sursa (job #2142088) | Cod sursa (job #2604898) | Cod sursa (job #2300231)
#include <fstream>
#include <vector>
std::ifstream f("heapuri.in");
std::ofstream g("heapuri.out");
class PriorityQueue {///comparasion after first ex {x, y}, ascending by x
struct element {
int first, second;
};
element* h;
int size;
void swap(element &x, element &y) {
element aux = x;
x = y;
y = aux;
}
public:
PriorityQueue(int maxSize) {
this->size = 0;
h = new element[maxSize];
}
element top() {
return h[1];
}
bool empty() {
return this->size == 0;
}
void push(element x) {
h[++size] = x;
int father = size / 2;
int son = size;
while (father) {
if (h[father].first > h[son].first)
this->swap(h[father], h[son]);
else
break;
son = father;
father /= 2;
}
}
void pop() {
h[1] = h[size];
--size;
int father = 1;
int son = 2;
while (son <= size) {
if (son + 1 <= size && h[son].first > h[son + 1].first)
++son;
if (h[father].first > h[son].first)
this->swap(h[father], h[son]);
else
break;
father = son;
son *= 2;
}
}
};
bool exists[MaxSize];
int main() {
PriorityQueue* h = new PriorityQueue(111);
int n;
f >> n;
int op, x;
int p = 0;
std::vector<bool> exists(n + 1, false);
for (int i = 1; i <= n; ++i) {
f >> op;
if (op == 1) {
f >> x;
h->push({ x, ++p });
continue;
}
if (op == 2) {
f >> x;
exists[x] = true;
continue;
}
while (!h->empty() && exists[h->top().second])
h->pop();
g << h->top().first << '\n';
}
return 0;
}