Arrange points

2.3

3 votes
Sorting, Algorithms, Easy, Merge Sort, Recruit, Merge sort, Approved
Problem

You are given N points (Xi,Yi) where 1iN. Print the N points in ascending order with respect to Xi. If there is more than one point with the same X value, then print the point that contains the larger Y value first.

Input format

  • First line: An integer N
  • Next N lines: Each line contains a point (Xi,Yi)

Output format

Print the N points in the same order illustrated in the question. The format of the output must be, "Xi Yi" without quotes. Each point must be printed in a new line.

Constraints

1N1000
1000Xi,Yi1000

Sample Input
5
2 5
3 6
1 2
3 5
5 1
Sample Output
1 2
2 5
3 6
3 5
5 1
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

First we sort the points according to the increasing X co-ordinate but as (3,6) and (3,5) have the same X co-ordinate we place (3,6) above (3,5) because it has a greater Y co-ordinate.

Editor Image

?