Under construction

Still being built. Things may change

Fangorn Forest

Time limit3sMemory limit128 MB

Summary
Find the smallest-angle oriented line through the rectangle's center and at least one tree that splits trees into two equal halves by the left-side rule.
Level

Medium7 of 10

Topics
Geometry, Sorting, Binary search, Implementation
Solved
No attempts yet

Problem

Ents are the guardians of the forest. Treebeard, the oldest Ent in Middle-earth, wants to decide which trees he will protect and which trees his younger fellow Ent, Bregalad, will protect.

The forest they guard is a rectangle, and it contains an even number of trees. Treebeard will draw a single straight line that splits the forest into two regions, keeping one for himself and giving the other to Bregalad. If the two regions differ in the number of trees or in area, the two Ents will quarrel for tens of thousands of years, so the split must be fair in both respects: each region must contain the same number of trees (exactly N/2N/2 each) and have the same area.

A tree lying exactly on the boundary line may be assigned to either region, but not to both.

Input

The input consists of several test cases. The first line of each test case contains the number of trees NN, the forest width WW, and the forest height HH. The four corners of the forest are (0,0)(0,0), (W,0)(W,0), (0,H)(0,H), and (W,H)(W,H). Each of the next NN lines contains a tree's coordinates x yx\ y.

  • 2≤N≤5000002 \le N \le 500000, and NN is even
  • 2≤W,H≤100002 \le W, H \le 10000, and WW and HH are never both even
  • All coordinates are integers with 0<x<W0 < x < W and 0<y<H0 < y < H
  • No two trees share the same position

Input ends with a line N=W=H=0N = W = H = 0, which must not be processed.

Output

Because several fair divisions may exist, output the one determined uniquely by the following rule.

The boundary line always passes through the center of the forest, (W2, H2)\left(\tfrac{W}{2},\ \tfrac{H}{2}\right) (any line through the center splits the rectangle into two equal areas). Require the line to pass through the center and through at least one tree. Give the line a direction, and let Treebeard's region be the trees strictly to its left, where 'left' means the side reached by turning 90°90° counterclockwise from the line's direction. Trees lying exactly on the line are added to Treebeard's region in increasing order of (x,y)(x, y) until his region holds exactly N/2N/2 trees.

If several directed lines through the center and at least one tree admit such a split (each side N/2N/2), choose the one whose direction angle, measured counterclockwise from the positive xx-axis in [0°,360°)[0°, 360°), is smallest.

For each test case, output the N/2N/2 trees of Treebeard's region, sorted by increasing xx and then by increasing yy, one per line in the format x y.

Examples1

  1. Example 1

    Input
    2 5 6
    2 3
    3 3
    4 5 6
    1 5
    2 5
    3 5
    4 5
    4 10 11
    5 1
    5 2
    5 3
    5 4
    0 0 0
    
    Expected output
    2 3
    1 5
    2 5
    5 1
    5 2