Dancers
InterviewTime limit1sMemory limit128 MB
Repeatedly pair the two closest unpaired dancers, breaking ties by smaller indices, and output all pairs sorted.
- Level
Medium5 of 10
- Topics
- Sorting, Geometry, Simulation, Implementation
- Solved
- No attempts yet
Problem
Alex missed a ballroom dance competition that he wanted to attend, so now he wants to reconstruct which pairs of dancers performed together. He has one photo of the competition in which every dancer is clearly visible, and from it he wrote down the coordinates of all N dancers (N is even).
Alex reconstructs the pairs with the following algorithm. Among the dancers that are not yet paired, he repeatedly chooses the two dancers that are closest to each other (by Euclidean distance) and assumes they form a pair. If several unpaired pairs share the same minimum distance, he chooses the lexicographically smallest pair. Dancers are numbered from 1 to N, and inside a pair the dancer with the smaller number is written first, so a pair always has ; two pairs are compared first by and then by .
Help Alex determine all of the pairs.
Input
The first line contains an even integer N ().
Each of the next N lines contains two integers: the x and y coordinates of the i-th dancer. Every coordinate has absolute value less than .
Output
Output N/2 lines, one per pair. Each line contains the two numbers of the dancers in that pair, the smaller number first. The lines must be sorted in lexicographically ascending order.