Pagini recente » Cod sursa (job #1334424) | Cod sursa (job #2620366) | Cod sursa (job #1858276) | Cod sursa (job #1902597) | Cod sursa (job #2852875)
#include <fstream>
#define NMAX 200005
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int questions, type, x, order, heapSize;
bool deleted[NMAX];
pair <int, int> heap[NMAX];
void addToHeap(int x, int order) {
pair <int, int> node = {x, order};
heapSize++;
heap[heapSize] = node;
int poz = heapSize;
int father = poz / 2;
while (poz > 1 && heap[poz].first < heap[father].first) {
swap(heap[poz], heap[father]);
poz = father;
father = poz / 2;
}
}
pair <int, int> minHeap() {
return heap[1];
}
void deleteHeap() {
heap[1] = heap[heapSize];
heapSize--;
int poz = 1;
int child1 = poz * 2;
int child2 = poz * 2 + 1;
while (true) {
int mini = heap[poz].first;
int newPoz = poz;
if (child1 <= heapSize && heap[child1].first < mini) {
mini = heap[child1].first;
newPoz = child1;
}
if (child2 <= heapSize && heap[child2].first < mini) {
mini = heap[child2].first;
newPoz = child2;
}
if (newPoz == poz)
break;
swap(heap[poz], heap[newPoz]);
poz = newPoz;
child1 = poz * 2;
child2 = poz * 2 + 1;
}
}
int main()
{
f >> questions;
while (questions--) {
f >> type;
if (type == 1) {
f >> x;
order++;
addToHeap(x, order);
}
if (type == 2) {
f >> x;
deleted[x] = true;
}
if (type == 3) {
pair<int, int> valNow = minHeap();
while (deleted[valNow.second]) {
deleteHeap();
valNow = minHeap();
}
g << valNow.first << "\n";
}
}
return 0;
}