Rural Planning
Time limit5sMemory limit512 MB
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 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 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 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 .
Input
The first line contains the number of test cases . Each test case begins with a line containing , the number of posts. The posts are numbered from to . Each of the next lines contains two integers and , the coordinates of post .
- The posts sit at distinct points, and they do not all lie on one line.
Output
For each test case, print one line containing "Case #x: ", where is the test case number starting from 1, followed by distinct integers from to 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 , and by when two posts share the same . Call this the post order. Let be the first post in the post order and let be the last. Both are corners of the convex hull. Walk the hull boundary counterclockwise: the walk from to is the lower chain, and the walk from to , 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 and belong to both chains.
Now build two fences.
- Fence U: , then the rest of the upper chain up to , then every post outside the upper chain in decreasing post order.
- Fence D: , then the rest of the lower chain up to , 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 . Print that fence in the order just described, starting at . If both fences enclose the same area, print Fence U.
Notes
No fence can enclose more than , 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 , every order traces the same triangle.