This page is still under construction.

Parts of this page are still being built. What you see may change.

Building the Moat

Interview

Time limit1sMemory limit128 MB

Summary
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 NN 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

  • 8≤N≤50008 \le N \le 5000
  • Every coordinate is an integer with 1≤x,y≤450001 \le x, y \le 45000.
  • 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 (18,0)(18,0), (6,−6)(6,-6), (0,−5)(0,-5), (−3,−3)(-3,-3), (−17,0)(-17,0), (−7,7)(-7,7), (0,4)(0,4), and (3,3)(3,3), giving a total length of 70.8700576850888…70.8700576850888\ldots. Printed to two decimal places, the answer is 70.8770.87.

Input

  • Line 1: a single integer NN.
  • Lines 2 to N+1N+1: two space-separated integers XiX_i and YiY_i, the coordinates of one watering hole.

Output

  • A single line containing one number DD, the length of the shortest possible moat, printed to exactly two decimal places.

Examples1

  1. Example 1

    Input
    20
    2 10
    3 7
    22 15
    12 11
    20 3
    28 9
    1 12
    9 3
    14 14
    25 6
    8 1
    25 1
    28 4
    24 12
    4 15
    13 5
    26 5
    21 11
    24 4
    1 8
    
    Expected output
    70.87