#C8243. Queue Reconstruction

    ID: 52204 Type: Default 1000ms 256MiB

Queue Reconstruction

Queue Reconstruction

You are given a list of people, where each person is represented by a pair of integers \((h, k)\). Here, \(h\) denotes the height of the person and \(k\) indicates the number of people in front of them who have a height greater than or equal to \(h\).

The task is to reconstruct the queue so that each person is positioned in such a way that exactly \(k\) people standing in front of them have a height no less than \(h\). The natural strategy is to first sort the list in descending order by height. In the case of a tie, sort by \(k\) in ascending order, then insert each person into the queue at the index specified by their \(k\) value.

This procedure guarantees a valid queue construction: after processing all persons, for any person \((h, k)\) in the queue, the number of persons before them with height \(\geq h\) will indeed be \(k\).

inputFormat

The first line contains an integer (n), the number of people. Each of the following (n) lines contains two space-separated integers (h) and (k), where (h) is the height and (k) is the number of people in front with height (\geq h).

outputFormat

Output the reconstructed queue in (n) lines. Each line should contain two integers (h) and (k) separated by a space, representing a person in the correct order.## sample

6
7 0
4 4
7 1
5 0
6 1
5 2
5 0

7 0 5 2 6 1 4 4 7 1

</p>