Building the Moat
InterviewTime limit1sMemory limit128 MB
Given N distinct points with no three collinear, compute the perimeter of their convex hull and print it to two decimal places.
- Level
Medium6 of 10
- Topics
- Geometry, Sorting, Divide and conquer, Implementation
- Solved
- No attempts yet
Problem
Farmer John wants to protect his farm from invading, thirsty aardvarks by digging a moat around it. He has watering holes, each at a distinct integer coordinate, and he will dig the moat as straight segments that run between pairs of holes.
The moat must enclose every watering hole: each hole must lie on or inside the moat, and the moat must form a single closed loop. Digging is expensive, so Farmer John wants the moat to be as short as possible. Find the length of the shortest moat he can build.
(Equivalently, this length is the perimeter of the convex hull of the watering holes.)
Constraints
- Every coordinate is an integer with .
- All holes lie at distinct positions, and no three holes are collinear.
In the grid below, the 20 * marks are watering holes and the surrounding lines show the shortest loop that encloses them:
...*-----------------*......
../..........*........\.....
./.....................\....
*......................*\...
|..........*........*....\..
|*........................\.
|..........................*
*..........................|
.\*........................|
..\.....................*..|
...\........*............*.|
....\..................*...*
.....\..*..........*....../.
......\................../..
.......*----------------*...
Starting from the top edge, the segment displacements are , , , , , , , and , giving a total length of . Printed to two decimal places, the answer is .
Input
- Line 1: a single integer .
- Lines 2 to : two space-separated integers and , the coordinates of one watering hole.
Output
- A single line containing one number , the length of the shortest possible moat, printed to exactly two decimal places.