Pagini recente » Cod sursa (job #3195912) | Cod sursa (job #1650019) | Cod sursa (job #2322255) | Cod sursa (job #1863225) | Cod sursa (job #2289760)
#include <iostream>
#include <fstream>
#include <vector>
std::ifstream f("heapuri.in");
std::ofstream g("heapuri.out");
#define MaxSize 200001
class MinHeap {
private:
int size;
int father(int node) {
return node / 2;
}
int leftSon(int node) {
return node * 2;
}
int rightSon(int node) {
return node * 2 + 1;
}
void sift(int k) {
int son;
do {
son = 0;
int lSon = leftSon(k);
if (lSon <= size) {
son = lSon;
int rSon = rightSon(k);
if (rSon <= size && h[rSon] < h[lSon]) {
son = rSon;
}
if (h[son] >= h[k]) {
son = 0;
}
}
if (son) {
std::swap(h[son], h[k]);
k = son;
}
} while (son);
}
void percolate(int k) {
int key = h[k];
int kFather = father(k);
while (k > 1 && key < h[kFather]) {
h[k] = h[kFather];
k = kFather;
kFather = father(k);
}
h[k] = key;
}
public:
int h[MaxSize];
MinHeap(int size){
this->size = size;
}
int getMin() {
return h[1];
}
int getSize() {
return this->size;
}
void insert(int value) {
h[++size] = value;
percolate(size);
}
void remove(int position) {
h[position] = h[size];
--size;
if (position > 1 && h[position] < h[father(position)])
percolate(position);
else
sift(position);
}
};
int poz[MaxSize];
int main() {
MinHeap* h = new MinHeap(0);
int n;
f >> n;
int op;
int x;
int p = 0;
for (int i = 1; i <= n; ++i) {
f >> op;
if (op == 1) {
f >> x;
h->insert(x);
poz[++p] = x;
continue;
}
if (op == 3) {
g << h->getMin() << '\n';
continue;
}
f >> x;
int sz = h->getSize();
int j;
for (j = 1; j <= sz; ++j)
if (h->h[j] == poz[x])
break;
h->remove(j);
}
return 0;
}