Pagini recente » Cod sursa (job #3207486) | Cod sursa (job #932522) | Borderou de evaluare (job #141010) | Cod sursa (job #1002403) | Cod sursa (job #2593905)
#include <stdio.h>
#include <stdlib.h>
#define DIM 200010
int noTests, type, nHeap, nInput, x;
int heap[DIM];
int pos[DIM];
int values[DIM];
int getMax(){
return values[heap[1]];
}
void swapNodes(int a, int b){
int aux;
aux = heap[a];
heap[a] = heap[b];
heap[b] = aux;
aux = pos[heap[a]];
pos[heap[a]] = pos[heap[b]];
pos[heap[b]] = aux;
}
void sift(int node){
int left = 2 * node;
int right = 2 * node + 1;
int minNode = node;
if(left <= nHeap && values[heap[left]] < values[heap[minNode]])
minNode = left;
if(right <= nHeap && values[heap[right]] < values[heap[minNode]])
minNode = right;
swapNodes(node, minNode);
}
int percolate(int node){
while(node > 1){
int father = node / 2;
if(values[heap[father]] > values[heap[node]]){
swapNodes(node, father);
node = father;
}
else{
break;
}
}
return node;
}
int main()
{
FILE *in = fopen("heapuri.in", "r");
FILE *out = fopen("heapuri.out", "w");
fscanf(in, "%d", &noTests);
for(int crtTest = 1; crtTest <= noTests; ++ crtTest){
fscanf(in, "%d", &type);
switch(type){
case 1:
fscanf(in, "%d", &values[++nInput]);
pos[nInput] = ++nHeap;
heap[nHeap] = nInput;
percolate(nHeap);
break;
case 2:
fscanf(in, "%d", &x);
int position = pos[x];
swapNodes(pos[x], nHeap);
nHeap --;
position = percolate(position);
sift(position);
break;
case 3:
fprintf(out, "%d\n", getMax());
break;
}
}
return 0;
}