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 N 2000001

int n=1;

void insert(int *,int);
int extractMax(int  *);
void heapIncreaseKey(int *,int,int);
void my_maxheapify(int *,int);
void my_maxbuild(int *);

int main(){
  int *a,in;
  char input[8];
  a=malloc(sizeof(int)*(N+1));
  while(1){
    scanf("%s",input);
    if(strcmp(input,"insert")==0){
      //INSERT
      scanf("%d",&in);
      insert(a,in);
    }
    if(strcmp(input,"extract")==0){
      //EXTRACT
      extractMax(a);
    }
    if(strcmp(input,"end")==0) break;
  }
  return 0;
}

void my_maxheapify(int *a,int i){
  int l,r,large;
  l=i*2;
  r=i*2+1;
  if(l<=n && a[l]>a[i]) large=l;
  else large=i;
  if(r<=n && a[r]>a[large]) large=r;
  if(large!=i){
    l=a[i];a[i]=a[large];a[large]=l;
    my_maxheapify(a,large);
  }
}

void my_maxbuild(int *a){
  int i;
  for(i=n/2; i>=1; i--){
    my_maxheapify(a,i);
  }
}

void insert(int *a,int k){
  n++;
  a[n]=-9999;
  heapIncreaseKey(a,n,k);
}

int extractMax(int *a){
  if(n<1) return -1;
  int max;
  max=a[1];
  a[1]=a[n];
  n--;
  my_maxheapify(a,1);
  printf("%d\n",max);
  return max;
}

void heapIncreaseKey(int *a,int i,int key){
  int t;
  if(key<a[i]) return;
  a[i]=key;
  while(i>1 && a[i/2]<a[i]){
    t=a[i/2];a[i/2]=a[i];a[i]=t;
    i=i/2;
  }
}