The Rubber Band and the Pegs

medium geometry sorting math 2000 ms · 256 MB

The Rubber Band and the Pegs

A carpenter hammers n pegs into a flat board, each at an integer-coordinate point (x, y). Two or more pegs may land at the exact same point. The carpenter then stretches an elastic rubber band around the outside of all the pegs and lets it snap taut, so it forms the smallest loop that still encloses every peg.

A peg touches the band if the band passes directly over it — this includes every peg sitting at a corner of the loop, but also every peg that happens to lie exactly on a straight stretch of the band between two corners, even though the band doesn't bend there. Pegs strictly inside the loop are not touched.

Two special cases:

  • If n = 1, the band has nowhere to stretch, so it collapses onto that single peg. That peg touches the band, and the band's length is 0.
  • If every peg lies on one straight line, the band can't form a loop around them — it stretches taut from the peg furthest in one direction to the peg furthest in the other direction and back, covering that segment twice. Every peg touches the band in this case, since every peg lies somewhere on that segment.

Given the positions of all n pegs, determine how many of them touch the band, and the total length of the band.

Input

The first line contains one integer n (1 ≤ n ≤ 2·10^5).

Each of the next n lines contains two integers x y (-10^9 ≤ x, y ≤ 10^9) — the position of a peg. Pegs may repeat.

Output

Print two lines.

On the first line, print one integer — the number of pegs that touch the band.

On the second line, print one real number — the length of the band. Your answer is accepted if its absolute or relative error is at most 10^-6.

Notes

In the first example, five pegs form a square with one extra peg sitting in the middle of the top edge; that extra peg touches the band even though the band runs straight through it, so 5 of the 6 pegs touch the band.

Answers within 1e-06 absolute or relative error are accepted.

Example 1

Input

6
0 0
4 0
4 4
0 4
2 4
2 2

Output

5
16.0

Example 2

Input

4
0 0
1 1
3 3
5 5

Output

4
14.142135623730951

Hints · reveal one at a time if you're stuck

Hint 1

The band is the boundary of the convex hull of the pegs. Start by removing duplicate positions, and think about what the hull looks like when there is only one distinct point or when all points lie on one line.

Hint 2

A standard hull routine drops points that lie on an edge, but here those pegs still touch the band. Modify the usual hull construction so collinear points on the boundary are kept, then count every peg on that boundary, using multiplicities.

Hint 3

For the length, only the true corners matter: the distance between consecutive corners adds up to the perimeter, and collinear points between two corners do not change it. The collinear case is a segment traversed in both directions, so its length is twice the span between its endpoints.

report an issue Prefer your own editor? sooks prep 93 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.