#C4369. Find K Closest Points to the Origin
Find K Closest Points to the Origin
Find K Closest Points to the Origin
Given a list of n points with integer coordinates, your task is to find the k points closest to the origin (0, 0). The distance is calculated using the Euclidean distance formula: \(\sqrt{x^2 + y^2}\). In the event of a tie (i.e. two points having the same distance), sort the points by their x-coordinate and then by their y-coordinate.
If k exceeds the total number of points, output all points sorted according to the criteria above. If k equals 0, output nothing.
inputFormat
The input is read from stdin
and has the following format:
- The first line contains two integers, n (the number of points) and k (the number of closest points to find).
- The next n lines each contain two space-separated integers representing the x and y coordinates of a point.
outputFormat
The output is written to stdout
and should contain exactly k lines (or all points if k exceeds n). Each line must contain two integers representing the x and y coordinates of one of the closest points to the origin, sorted in increasing order of their Euclidean distance. For points with the same distance, they must be sorted by x-coordinate, then by y-coordinate.
4 2
1 3
-2 2
5 8
0 1
0 1
-2 2
</p>