In connection with the modernization of production at the toothbrush factory in Tau…
- User
In connection with the modernization of production at the toothbrush factory in Tau Kita, it was decided to rewrite the list of robots that serve the plant. Each robot has two numbers: main and auxiliary. The new list must satisfy the following rules:
If one robot in the new list is earlier than the other, then the main number of the first robot is less than or equal to the main number of the second robot.
If the main numbers of the robots are equal, they are placed in the same order as in the given list.
Tau Chinese have asked you to rewrite the list. Help modernize the organization!
Input data The first line of the input file contains an integer N (1 ≤ N ≤ 100000) - the number of robots in the factory. Each subsequent line contains 2 numbers - the primary and secondary numbers of the next robot. Both numbers are nonnegative and do not exceed 10^9.
Output. Print N lines, the i-th line contains 2 numbers - the main and auxiliary number of the i-th robot in the new list.
Examples Input #1 10 1 8 8 9 2 10 1 11 4 2 7 2 3 11 2 23 3 3 6 7 Answer #1 1 8 1 11 2 10 2 23 3 11 3 3 4 2 6 7 7 2 8 9
- Assistant
To solve this problem, we need to sort the robots based on their primary numbers in non-decreasing order while preserving the original order of robots with the same primary number. This is a classic case where we need a stable sort.
Here is the step-by-step plan:
-
Read the Input: Read all the robots and store them along with their original indices. This index will help us preserve the order of robots with the same primary number.
-
Stable Sort: Use a stable sorting algorithm to sort the robots based on their primary numbers. A stable sort ensures that if two elements have the same key, their order relative to each other is preserved from the original input.
-