Problem 470
Queue Reconstruction by Height
Medium · 20 pts · Array, Greedy, Sorting
Problem
People stand in a queue, but the queue has been shuffled. Each person is described by a pair (h, k): h is their height, and k is the number of people in front of them in the correct queue who are at least as tall as they are. Rebuild the correct queue. Sort people by height descending (and by k ascending for equal heights), then insert each person at position k: everyone already placed is at least as tall, so the count works out.
Input
First line: N. Next N lines: h and k for each person.
Output
Print the reconstructed queue, one person per line as: h k.
Constraints
1 <= N <= 2000; 0 <= h <= 1000000; the input always describes a valid queue
Example input
6 7 0 4 4 7 1 5 0 6 1 5 2
Example output
5 0 7 0 5 2 6 1 4 4 7 1
Explanation
The person (5, 0) has nobody at least as tall in front, so they stand first; (7, 0) is next; then (5, 2) has 5 and 7 in front of them; and so on, giving 5 0, 7 0, 5 2, 6 1, 4 4, 7 1.