A warehouse dock receives pallets one at a time, in a fixed arrival order. Each pallet carries an integer priority tag (duplicates are possible, and tags may be negative). As each pallet arrives, warehouse workers place it directly onto a single-file storage line: they may only walk forward along the line from its entrance, one pallet at a time, so a new pallet is always slotted into the line at the point that keeps the whole line sorted by priority tag from smallest (nearest the entrance) to largest, before the next pallet is allowed to arrive.
Given the pallets in their arrival order, determine the priority tags of the final storage line, from the entrance outward, after every pallet has been placed.
Line 1: one integer n, the number of pallets.
Line 2 (present only when n > 0): n integers, the pallets' priority tags in arrival order.
A single line containing the n priority tags of the final storage line, from the entrance outward, in non-decreasing order, separated by single spaces (an empty line if n = 0).
0 <= n <= 5000-5000 <= tag <= 5000Example 1
Input
5 4 2 1 3 5
Expected
1 2 3 4 5
Explanation
The pallets arrive in order 4, 2, 1, 3, 5. Each is inserted into the already-sorted line at the point that keeps it sorted: after all five arrivals the line reads 1 2 3 4 5 from the entrance outward.
Example 2
Input
6 3 -1 3 0 -1 2
Expected
-1 -1 0 2 3 3
Explanation
Sorting the six arriving tags 3, -1, 3, 0, -1, 2 in non-decreasing order, keeping both copies of -1 and both copies of 3, gives -1 -1 0 2 3 3.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →