#FT0000050. Khôi phục hàng đợi theo chiều cao (Queue Reconstruction by Height)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Khôi phục hàng đợi theo chiều cao (Queue Reconstruction by Height)
Queue Reconstruction by Height
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
The original order of a queue of people has been lost. For each person, you are given a pair :
- is their height.
- is the number of people before them in the correct queue whose heights are greater than or equal to .
Reconstruct a queue using all given people so that every person's requirement is satisfied. The front position is position , but no positions should be printed. If several queues are valid, any one is acceptable; each input pair must be used with exactly its given multiplicity.
At least one valid reconstruction is guaranteed.
Input
- Line 1: integer , the number of people.
- Next lines: two integers describing one person per line; the input order is not the final queue order.
Output
Print exactly lines from the front to the back of the queue, each containing a person’s integers . Use exactly the supplied multiset of pairs and satisfy each person’s count of earlier people with height at least .
Subtasks
Subtask 1 (20 points):
- ; ; for every ; a valid arrangement of all people exists.
Subtask 2 (30 points):
- ; ; for every ; a valid arrangement of all people exists.
Subtask 3 (50 points):
- ; ; for every ; a valid arrangement of all people exists.
Examples
Examples 1
Input
6
7 0
4 4
7 1
5 0
6 1
5 2
Output
5 0
7 0
5 2
6 1
4 4
7 1
Explanation
In the output queue, has nobody before it. has no previous person of height at least . has two preceding people of height at least . has only the preceding height- person. has four qualifying predecessors. The final has exactly one preceding person with height at least .
Examples 2
Input
3
6 0
5 0
4 0
Output
4 0
5 0
6 0
Explanation
In queue , each person is taller than all earlier people, so every person has zero qualifying predecessors.
Placing a person of height before someone of height or would give that later person at least one preceding person of greater or equal height, contradicting . Hence the valid increasing-height order is , , .