Under construction

Still being built. Things may change

Rural Planning

Time limit5sMemory limit512 MB

Summary
Arrange every post into a simple polygon larger than half the convex hull area by building the two specified hull-chain orders and keeping the larger one.
Level

Medium5 of 10

Topics
Geometry, Sorting, Implementation
Solved
No attempts yet

Problem

You bought a big farmyard and want to fence it in. The yard already holds NN fence posts, and your lawyers insist that the fence use every single one of them.

The posts are points in the plane. A fence is an order of all NN posts: you run a straight length of fence from the first post to the second, from the second to the third, and so on, and finally from the last post back to the first. The result must be a polygon that never touches itself. Exactly two fence segments meet at each post, and no other point of the plane lies on more than one segment.

You also want the yard to stay big. Let AA be the largest area you could enclose if you were allowed to leave some posts out, which is the area of the convex hull of the posts. Your fence has to enclose more than A/2A / 2.

Input

The first line contains the number of test cases TT. Each test case begins with a line containing NN, the number of posts. The posts are numbered from 00 to N−1N - 1. Each of the next NN lines contains two integers XiX_i and YiY_i, the coordinates of post ii.

  • The NN posts sit at distinct points, and they do not all lie on one line.
  • 1≤T≤301 \le T \le 30
  • 3≤N≤10003 \le N \le 1000
  • −50000≤Xi,Yi≤50000-50000 \le X_i, Y_i \le 50000

Output

For each test case, print one line containing "Case #x: ", where xx is the test case number starting from 1, followed by NN distinct integers from 00 to N−1N - 1 separated by single spaces. They are the posts in fence order, and the last post is joined back to the first.

Many orders give a legal fence, so the one to print is fixed by the following recipe.

Sort the posts by XX, and by YY when two posts share the same XX. Call this the post order. Let LL be the first post in the post order and let RR be the last. Both are corners of the convex hull. Walk the hull boundary counterclockwise: the walk from LL to RR is the lower chain, and the walk from RR to LL, read backwards, is the upper chain. A post that lies on a hull edge without being a corner belongs to the chain holding that edge, and LL and RR belong to both chains.

Now build two fences.

  • Fence U: LL, then the rest of the upper chain up to RR, then every post outside the upper chain in decreasing post order.
  • Fence D: LL, then the rest of the lower chain up to RR, then every post outside the lower chain in decreasing post order.

Neither fence touches itself, and the one enclosing the larger area always encloses more than A/2A / 2. Print that fence in the order just described, starting at LL. If both fences enclose the same area, print Fence U.

Notes

No fence can enclose more than AA, because every fence stays inside the convex hull of the posts.

The two fences often enclose the same area. On any input where every post lies on the hull boundary, both fences trace the hull itself. When N=3N = 3, every order traces the same triangle.

Examples2

  1. Example 1

    Input
    3
    4
    1 2
    2 0
    0 0
    1 1
    5
    0 0
    1 1
    2 2
    0 2
    2 0
    3
    0 0
    1 0
    0 1
    
    Expected output
    Case #1: 2 1 0 3
    Case #2: 0 3 2 4 1
    Case #3: 0 2 1
    
  2. Example 2

    Input
    4
    3
    0 0
    4 0
    0 3
    5
    0 0
    4 0
    4 4
    0 4
    2 2
    4
    0 0
    2 0
    4 0
    0 4
    6
    -2 -1
    0 -2
    2 -1
    2 1
    0 2
    -2 1
    
    Expected output
    Case #1: 0 2 1
    Case #2: 0 3 2 1 4
    Case #3: 0 3 2 1
    Case #4: 0 5 4 3 2 1