← 返回 pinterest 的题目列表Reconstruct a Queue by Height (Laundry Sorting Variant) + Distance Between Any Two Stations
类型:online_judge
Problem: Reconstruct the “Laundry Queue” (LeetCode 406 variant) + Station Distance Queries
Part A: Reconstruct the order
You are given n items, each represented as (h, k):
h: an attribute value (think of it as height/priority)
k: in the final ordering, the number of items in front of it with attribute value >= h.
Reconstruct and output any ordering that satisfies all constraints.
Input
First line: integer n.
Next n lines: two integers h k.
Output
Print n lines: the reconstructed ordering, each line h k.
Constraints
1 <= n <= 1e4
0 <= h <= 1e9
0 <= k < n
At least one valid solution exists.
Part B (Follow-up): Distance between any two stations
Given station coordinates on a 1D line, answer queries for distance between station i and j.
Input
First line: integer m (#stations)
Second line: m integers x[i]
Third line: integer q (#queries)
Next q lines: two integers i j (0-index)
Output
For each query, output |x[i] - x[j]|.
Constraints
1 <= m,q <= 2e5
|x[i]| <= 1e9
Sample Tests (5)
(See the Chinese section for concrete input/output blocks.)
Example
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