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 <cstdio>
#include <istream>
using namespace std;

static const int MAX_NUM = 2000001;

void MakeHeap(int elem[], int *pNum, int target)
{
    int left, right, largest;

    left    = target * 2;
    right   = left + 1;
    largest = target;

    if (right <= *pNum) {
        if (elem[right] > elem[largest]) {
            largest = right;
        }
        if (elem[left] > elem[largest]) {
            largest = left;
        }
    }
    else {
        if (left <= *pNum) {
            if (elem[left] > elem[largest]) {
                largest = left;
            }
        }
    }

    if (largest != target) {
        swap(elem[largest],elem[target]);
        MakeHeap(elem, pNum, largest);
    }

    return;
}

int ExtractMax(int elem[], int *pNum)
{
    int ret = elem[1];
    int now, left, right, target;

    if (*pNum >= 1) {
        swap(elem[1], elem[*pNum]);
        --(*pNum);

        MakeHeap(elem, pNum, 1);
    }
    else {
        ret = -(1 << 30);
    }

    return ret;
}

void Insert(int elem[], int *pNum, int target)
{
    int id;
    int parent;

    ++(*pNum);
    id = *pNum;
    elem[id] = target;

    parent = id / 2;
    while (parent > 0) {
        if (elem[id] > elem[parent]) {
            swap(elem[id], elem[parent]);
            id = parent;
            parent = id / 2;
        }
        else {
            break;
        }
    }

    return;
}

int main(void)
{
    int  elem[MAX_NUM];
    int  num = 0;
    int  key;
    char order[10];

    elem[1] = -1;

    for (;;) {
        scanf("%s", order);
        if (order[0] == 'e') {
            if (order[1] == 'n') {
                break;
            }
            else {
                key = ExtractMax(elem, &num);
                printf("%d\n", key);
            }
        }
        else if (order[0] == 'i') {
            scanf("%d", &key);
            Insert(elem, &num, key);
        }
    }

    return 0;
}