Pagini recente » Cod sursa (job #1477864) | Cod sursa (job #3206904) | Cod sursa (job #695195) | Cod sursa (job #999517) | Cod sursa (job #2636413)
#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];
swap(heap[i], heap[m]);
swap(t[i], t[m]);
pos[t[i]] = i;
pos[t[m]] = -1;
--m;
while (i > 1 && heap[i] < heap[i / 2]) {
swap(heap[i], heap[i / 2]);
swap(t[i], t[i / 2]);
pos[t[i]] = i;
pos[t[i / 2]] = i / 2;
i /= 2;
}
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");
for (int i = 1;i <= nr;++i)
printf("%i ", pos[i]);
printf("\n\n");
}
int main() {
fin = fopen("heapuri.in", "r");
fout = fopen("heapuri.out", "w");
fscanf(fin, "%i", &n);
int l = 0;
while (n--) {
++l;
int t, x;
fscanf(fin, "%i", &t);
if (t == 3) {
int t = top();
fprintf(fout, "%i\n", t);
}
else {
fscanf(fin, "%i", &x);
if (t == 1) {
++nr;
push(x);
}
else {
pop(x);
}
}
//afis();
}
return 0;
}