869. The k Nearest Points
Given n points and a number k, print the k points closest to the origin,
in increasing order of distance. Break ties by smaller x, then by smaller y.
Print one point per line as two space-separated integers.
Sorting everything costs O(n log n) and produces n ordered points when only
k were wanted. Keeping a heap of size k costs O(n log k), which matters
when k is small and n is large: hold the k best so far, and discard the
worst of them whenever something better arrives.
Compare SQUARED distances. The square root is a strictly increasing function, so it cannot change any ordering, and computing it introduces floating point where integers would do.
Constraints - `1 ≤ k ≤ n ≤ 100000` - `-10000 ≤ x, y ≤ 10000`
Input
The first line contains two integers n and k.
Each of the next n lines contains two integers: the x and y of one point.
Output
Print the k closest points, one per line, each as two space-separated integers, in increasing order of distance with ties broken by smaller x then smaller y.
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.