Pagini recente » Cod sursa (job #2226393) | Cod sursa (job #2644212) | Cod sursa (job #317156) | Cod sursa (job #3265012) | Cod sursa (job #807131)
Cod sursa(job #807131)
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 200010
#define inf 1000000010
int N, M;
int H[maxn], P[maxn], E[maxn];
void PUSH(int x, int p) {
N ++;
H[N] = x;
E[N] = p;
P[p] = N;
int pos = N;
while(pos > 1) {
int father = pos / 2;
if(H[pos] < H[father]) {
swap(H[pos], H[father]);
P[E[father]] = pos;
P[E[pos]] = father;
swap(E[pos], E[father]);
}
else {
break;
}
pos = father;
}
}
void POP(int x) {
swap(H[N], H[x]);
P[E[N]] = x;
P[E[x]] = N;
swap(E[N], E[x]);
N --;
while(x < N) {
int next = 0;
int val = inf;
if(2*x <= N && H[2*x] < val) {
next = 2*x;
val = H[2*x];
}
if(2*x+1 <= N && H[2*x+1] < val) {
next = 2*x+1;
val = H[2*x+1];
}
if(next == 0) break;
swap(H[x], H[next]);
P[E[x]] = next;
P[E[next]] = x;
swap(E[x], E[next]);
x = next;
}
}
int main() {
FILE * f1 = fopen("heapuri.in", "r"), * f2 = fopen("heapuri.out", "w");
int i = 0, op, x;
fscanf(f1, "%d\n", &M);
while(M--) {
fscanf(f1, "%d", &op);
if(op == 1) {
fscanf(f1, "%d", &x);
i ++;
PUSH(x, i);
}
else if(op == 2) {
fscanf(f1, "%d", &x);
POP(P[x]);
}
else if(op == 3) {
fprintf(f2, "%d\n", H[1]);
}
}
fclose(f1); fclose(f2);
return 0;
}