Laser Sensors
Time limit2sMemory limit512 MB
Given N blue points and 2N red points in general position, build the particular non-crossing perfect matching prescribed by the paper's recursive angular-sweep Solve/Attach procedure.
- Level
Hard9 of 10
- Topics
- Divide and conquer, Geometry, Sorting, Recursion
- Solved
- No attempts yet
Problem
A precision measurement company bought several devices that measure the movement of a distant object with a laser and installed them at different places. Every device carries two laser sensors and one sensor measures one object, so a single device measures two objects at the same time. Laser beams sent from two different devices interfere with each other where they meet, and that interference produces measurement errors, so the beams are arranged so that they never cross.
Draw every installed device as a blue point of the plane and every measured object as a red point. The task becomes the following.
The plane holds blue points and red points. Call a set of segments a linking when it satisfies all three conditions.
- Every blue point is joined to exactly two red points.
- Every red point is joined to exactly one blue point.
- No two segments cross.

Part (A) of the figure holds three blue points and six red points. Blue point 1 is joined to red points 1 and 4, blue point 2 to red points 2 and 5, blue point 3 to red points 3 and 6, and no two segments cross, so (A) is a linking. Part (B) is another linking of the same points, so one input can admit several linkings. In part (C) the segment from blue point 1 to red point 3 crosses the segment from blue point 3 to red point 2, so (C) is not a linking.
One input can admit several linkings, so print the one that the procedure in the output section builds.
Input
The first line holds the number of blue points (). The -th of the next lines holds the x coordinate and the y coordinate of blue point . The -th of the following lines holds the x coordinate and the y coordinate of red point . Every coordinate is an integer between and . No three of the given points lie on one line.
Output
Print lines. Line holds the numbers of the two red points joined to blue point , the smaller number first, separated by one space.
Several linkings can exist, so print the one that the procedure below builds. The procedure always finds the index it asks for, and the linking it builds always satisfies the three conditions.
Give every blue point the value and every red point the value , and let the value of a set of points be the sum of the values of its points. The given points have value . Run Solve on all given points.
Solve(), where the value of is :
- If is empty, stop.
- Let be the point of with the smallest y coordinate, and among such points the one with the smallest x coordinate. List the other points of as in increasing order of the angle of the vector that runs from to the point, measured counterclockwise from the positive direction of the x axis. Every one of these angles is at least and smaller than , and no two of them are equal.
- Let and , where is the value of .
- If is blue, let be the smallest index with and let be the smallest index with . Join to and to . Then run Solve on , on , and on .
- If is red, let be the largest index with . The point is blue. Join to . If , run Attach(; ; ) and Solve(). Otherwise run Solve() and Attach(; ; ).
Attach(; ; ), where is not empty and has value , the blue point still needs one red point, and is a point outside :
- List the points of as in increasing order of the angle at between the ray from to and the ray from to the point. Every one of these angles is greater than and smaller than , and no two of them are equal.
- Let and , where is the value of .
- Let be the largest index with . The point is red. Join to . Then run Solve on and on .