problems / 258

Closest Huddle of Drifting Buoys

medium ternary-search math 1000 ms · 256 MB

Closest Huddle of Drifting Buoys

n buoys drift along a straight channel, which we treat as a number line. At time 0 buoy i is at position x_i, and it drifts with a constant speed v_i (a negative speed means it drifts toward smaller positions). So at time t buoy i is at position x_i + v_i·t. Buoys never interact: they pass through each other freely.

The spread of the buoys at time t is the distance between the two outermost buoys, that is the largest position minus the smallest position at that moment.

Find the smallest spread over all real times t ≥ 0.

Input

The first line contains n (1 ≤ n ≤ 10^5), the number of buoys.

Each of the next n lines contains two integers x_i and v_i (−10^5 ≤ x_i ≤ 10^5, −1000 ≤ v_i ≤ 1000): the starting position and the speed of buoy i. Several buoys may share a position or a speed.

Output

Print the smallest spread over all times t ≥ 0, a real number. Your answer is accepted if its absolute or relative error is at most 10^-6.

Notes

In the first example the buoys are at 2t, 10 − t and 4. At time t = 10/3 the first two meet at position 20/3, and the spread is 20/3 − 4 = 8/3; at no other moment is it smaller.

In the second example the buoys only move apart, so the smallest spread is the one at time 0. Times before 0 don't count.

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

Example 1

Input

3
0 2
10 -1
4 0

Output

2.6666666667

Example 2

Input

2
0 -1
5 3

Output

5.0000000000

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

Hint 1

Plot each buoy's position against time: it's a straight line. What shape does the curve "highest line minus lowest line" have?

Hint 2

The top boundary of a set of lines is convex and the bottom boundary is concave, so the spread is a convex function of t: it goes down (or stays flat) and then goes up. You can evaluate it at any single t in O(n).

Hint 3

Minimize a convex (unimodal) function over a real interval with ternary search. Every crossing of two buoys happens by time 2·10^5, so that's a safe right end.

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

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