Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Priority Queue

A priority queue is a data structure which maintains a set $S$ of elements, each of with an associated value (key), and supports the following operations:

$insert(S, k)$: insert an element $k$ into the set $S$

$extractMax(S)$: remove and return the element of $S$ with the largest key

Write a program which performs the $insert(S, k)$ and $extractMax(S)$ operations to a priority queue $S$.
The priority queue manages a set of integers, which are also keys for the priority.

Input

Multiple operations to the priority queue $S$ are given. Each operation is given by "insert $k$", "extract" or "end" in a line. Here, $k$ represents an integer element to be inserted to the priority queue.

The input ends with "end" operation.

Output

For each "extract" operation, print the element extracted from the priority queue $S$ in a line.

Constraints

The number of operations $\leq 2,000,000$

$0 \leq k \leq 2,000,000,000$

Sample Input 1

insert 8
insert 2
extract
insert 10
extract
insert 11
extract
extract
end

Sample Output 1

8
10
11
2

Solution in C (judge verdict: Accepted)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define HEAP_MAX  2000000

#define left(x)   (2*x+1)
#define right(x)  (2*x+2)
#define parent(x) ((x-1)/2)

typedef struct _priorityQueue
{
  int heap_size;
  int queue[HEAP_MAX];
} priorityQueue;

void swap(int *a, int *b)
{
  int temp = *a;
  *a = *b;
  *b = temp;
}

void initPriorityQueue(priorityQueue *pq)
{
  pq->heap_size = 0;
}

int insert(priorityQueue *pq, int key)
{
  int i = pq->heap_size;

  if (pq->heap_size == HEAP_MAX) {
    printf("heap full.\n");
    exit(-1);
  }

  pq->queue[(pq->heap_size)++] = key;
  
  //   j      j
  //  /   or   \
  // i          i
  // gurantees that i <= j.
  while (i > 0 && pq->queue[i] > pq->queue[parent(i)]) {
    swap(&pq->queue[i], &pq->queue[parent(i)]);
    i = parent(i);
  }

  return 0;
}

void printPriorityQueue(priorityQueue *pq)
{
  int i;
  printf("%d\n", pq->heap_size);
  for (i=0; i<pq->heap_size; i++)
    printf("%d\n", pq->queue[i]);
}

// maxHeapify gurantees that heap structure is stil valid.
//     i
//    / \
//   l   r
// i >= l && i >= r.
void maxHeapify(priorityQueue *pq, int i, int l, int r)
{
  int max, maxi;

  //  This is when i is a leaf.
  if (r > pq->heap_size)
    return;

  // swap i and the larger of l or r, then call maxHeapify recursively.
  if (pq->queue[i] < pq->queue[l] && pq->queue[i] < pq->queue[r]) {
    max = (pq->queue[l] > pq->queue[r] ? pq->queue[l] : pq->queue[r]);
    maxi = (max == pq->queue[l] ? l : r);
    swap(&pq->queue[i], &pq->queue[maxi]);
    maxHeapify(pq, maxi, left(maxi), right(maxi));
  } else if (pq->queue[i] < pq->queue[l]) {
    swap(&pq->queue[i], &pq->queue[l]);
    maxHeapify(pq, l, left(l), right(l));
  } else if (pq->queue[i] < pq->queue[r]) {
    swap(&pq->queue[i], &pq->queue[r]);
    maxHeapify(pq, r, left(r), right(r));
  }
}

int extractMax(priorityQueue *pq)
{
  int max;

  if (pq->heap_size < 1) {
    perror("heap is empty.");
    return -1;
  }

  max = pq->queue[0];
  pq->queue[0] = pq->queue[--(pq->heap_size)];

  // call maxHeapify from the root.
  maxHeapify(pq, 0, 1, 2);

  return max;
}

int main(int argc, char const *argv[])
{
  char input[10];
  int n;
  priorityQueue pq;
  initPriorityQueue(&pq);
  
  while (1) {
    scanf("%s", input);
    if (!strcmp(input, "insert")) {
      scanf("%d", &n);
      insert(&pq, n);
    } else if (!strcmp(input, "extract")) {           
      n = extractMax(&pq);
      printf("%d\n", n);
    } else {
      break;
    }  
  }

//  printPriorityQueue(&pq);
  return 0;
}