Pagini recente » Cod sursa (job #1534971) | Cod sursa (job #925385) | Cod sursa (job #1142789) | Cod sursa (job #943008) | Cod sursa (job #2636409)
#include <stdio.h>
#define NMAX 200005
FILE* fin, * fout;
int heap[NMAX], pos[NMAX], t[NMAX], n, m = 0, nr = 0;
void swap(int& x, int& y) {
int aux = x;
x = y;
y = aux;
}
void push(int x) {
heap[++m] = x;
pos[nr] = m;
t[m] = nr;
x = m;
while (x > 1 && heap[x] < heap[x / 2]) {
swap(heap[x], heap[x / 2]);
swap(t[x], t[x / 2]);
pos[t[x]] = x;
pos[t[x / 2]] = x / 2;
x /= 2;
}
}
void pop(int i) {
i = pos[i];
heap[i] = heap[m];
t[i] = t[m];
pos[t[i]] = i;
--m;
while (i <= m) {
int left = 2 * i, right = 2 * i + 1, min = i;
if (left <= m && heap[left] < heap[min])
min = left;
if (right <= m && heap[right] < heap[min])
min = right;
if (min == i) return;
swap(heap[i], heap[min]);
swap(t[i], t[min]);
pos[t[i]] = i;
pos[t[min]] = min;
i = min;
}
}
int top() {
return heap[1];
}
void afis() {
for (int i = 1;i <= m;++i)
printf("%i ", heap[i]);
printf("\n");
}
int main() {
fin = fopen("heapuri.in", "r");
fout = fopen("heapuri.out", "w");
fscanf(fin, "%i", &n);
while (n--) {
int t, x;
fscanf(fin, "%i", &t);
if (t == 3) {
fprintf(fout, "%i\n", top());
}
else {
fscanf(fin, "%i", &x);
if (t == 1) {
++nr;
push(x);
}
else {
pop(x);
}
}
//afis();
}
return 0;
}