Pagini recente » Cod sursa (job #631751) | Cod sursa (job #2114516) | Cod sursa (job #855058) | Cod sursa (job #1846269) | Cod sursa (job #335278)
Cod sursa(job #335278)
#include <stdio.h>
#define DIM 500000
int H[DIM];
int V[DIM/2];
int P[DIM];
int N, dH, dV, x, op, i;
void up(int c){
int p = c/2;
int aux;
while (p && V[H[p]]>V[H[c]]) {
aux = H[p];
H[p] = H[c];
H[c] = aux;
P[H[p]] = p;
P[H[c]] = c;
c = p;
p = c/2;
}
}
void down(){
int p = 1;
int c = 2;
int aux;
while (c<=dH) {
if (c+1 <= dH && V[H[c]]>V[H[c+1]])
c++;
if (V[H[c]]<V[H[p]]) {
aux = H[p];
H[p] = H[c];
H[c] = aux;
P[H[p]] = p;
P[H[c]] = c;
p = c;
c = 2*p;
} else break;
}
}
int main(){
FILE *f = fopen("heapuri.in","r");
FILE *g = fopen("heapuri.out","w");
fscanf(f,"%d",&N);
for (i=1;i<=N;i++) {
fscanf(f,"%d",&op);
if (op==1) { //inserare
fscanf(f,"%d",&x);
V[++dV] = x;
dH++;
H[dH] = dV;
P[dV] = dH;
up(dH);
} else if (op==2) { //sterg elementul de pe pozitia x
fscanf(f,"%d",&x);
V[x] = -1;
up(P[x]);
H[1] = H[dH];
P[H[1]] = 1;
dH--;
down();
} else {
fprintf(g,"%d\n",V[H[1]]);
}
}
fclose(f);
fclose(g);
return 0;
}