<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

204. Little Jumper
time limit per test: 0.5 sec.
memory limit per test: 65536 KB
input: standard
output: standard



Little frog Georgie likes to jump. Recently he have discovered the new playground that seems the perfect place to jump.

Recently the new jumping exercise has become very popular. Two vertical walls are placed on the playground, each of which has a hole.

The lower sides of the holes in the walls are on heights b1 and b2 respectively, and upper sides on heights t1 and t2. Walls are parallel and placed on distance l from each other.

The jumper starts at the distance ds from the first wall. It jumps through the first hole and lands between the walls. After that from that point he jumps through the second hole. The goal is to land exactly at the distance df from the second wall.





Let us describe the jump. The jumper starts from the specified point and starts moving in some chosen direction with the speed not exceeding some maximal speed v, determined by the strength of the jumper. The gravity of g forces him down, thus he moves along the parabolic trajectory.

The jumper can choose different starting speeds and different directions for his first and second jump.

Of course, The jumper must not attempt to pass through the wall, although it is allowed to touch it passing through the hole, this does not change the trajectory of the jump. The jumper is not allowed to pass through both holes in a single jump.

Find out, what must be the maximal starting speed of the jumper so that he could fulfil the excersise.


Input
Input file contains one or more lines, each of which contains eight real numbers, separated by spaces and/or line feeds. They designate b1, t1, b2, t2, l, ds, df and g. All numbers are in range from 10-2 to 103, t1≥ b1 + 10-2, t2≥ b2 + 10-2.

Output
For each line of the input file output the smallest possible maximal speed the jumper must have to fulfil the exercise. If it is impossible to fulfil it, output -1. Your answer must be accurate up to 10-4.

Sample test(s)

Input
0.3 1.0 0.5 0.9 1.7 1.2 2.3 9.8
0.6 0.8 0.6 0.8 2.4 0.3 1.5 0.7

Output
5.2883
1.3127
Author:	Andrew Stankevich
Resource:	Petrozavodsk Summer Trainings 2003
Date:	2003-08-23

<|response|>
## 1. Abridged problem statement

A frog must make exactly two projectile jumps:

1. It starts on the ground, `ds` before the first wall.
2. It jumps through the first wall’s hole, whose height interval is `[b1, t1]`, and lands somewhere between the two walls.
3. It then jumps through the second wall’s hole, whose height interval is `[b2, t2]`, and lands on the ground exactly `df` after the second wall.

The walls are distance `l` apart. Gravity is `g`.

For each jump, the frog may choose any direction and any starting speed not exceeding some maximum speed `v`.

Find the minimum possible `v`.

Input contains multiple test cases until EOF.

---

## 2. Key observations needed to solve the problem

### Observation 1: Fix the landing point between the walls

Suppose the frog lands at distance `p` after the first wall.

Then:

```text
0 < p < l
```

The two jumps become independent:

- First jump:
  - start to first wall: `ds`
  - first wall to landing point: `p`
  - hole: `[b1, t1]`

- Second jump:
  - landing point to second wall: `l - p`
  - second wall to final point: `df`
  - hole: `[b2, t2]`

For fixed `p`, we can compute the minimum speed needed for each jump separately.

The required maximum speed for this `p` is:

```text
max(speed_first(p), speed_second(p))
```

So the whole problem becomes:

```text
minimize over p in (0, l):
    max(speed_first(p), speed_second(p))
```

---

### Observation 2: One jump can be solved in closed form

Consider one jump:

- The frog starts at ground height `0`.
- The wall is at horizontal distance `d1`.
- The frog lands again at ground height `0` after another distance `d2`.
- The frog must cross the wall at some height `h` in `[b, t]`.

The total horizontal distance is:

```text
R = d1 + d2
```

Because the trajectory starts and ends at height `0`, its parabola has roots at `x = 0` and `x = R`.

So geometrically:

```text
y(x) = a * x * (R - x)
```

At the wall:

```text
h = y(d1) = a * d1 * d2
```

Therefore:

```text
a = h / (d1 * d2)
```

Projectile motion gives:

```text
y(x) = (vy / vx) * x - g * x^2 / (2 * vx^2)
```

Comparing the coefficient of `x^2`:

```text
a = g / (2 * vx^2)
```

After simplification, the required speed squared for crossing the wall at height `h` is:

```text
v^2(h) =
    g * d1 * d2 / (2h)
    +
    g * h * (d1 + d2)^2 / (2 * d1 * d2)
```

This has the form:

```text
v^2(h) = A / h + B * h
```

where:

```text
A = g * d1 * d2 / 2
B = g * (d1 + d2)^2 / (2 * d1 * d2)
```

---

### Observation 3: The best crossing height is easy to find

The function

```text
A / h + B * h
```

is convex for `h > 0`.

Its unconstrained minimum is at:

```text
h = sqrt(A / B)
```

After simplification:

```text
h = d1 * d2 / (d1 + d2)
```

But the frog must pass through the hole, so we clamp this height to the hole interval:

```text
h = clamp(d1 * d2 / (d1 + d2), b, t)
```

Then we evaluate `v^2(h)`.

---

### Observation 4: Optimize the landing point using ternary search

For each landing point `p`, define:

```text
cost(p) = max(
    required speed squared for first jump,
    required speed squared for second jump
)
```

This function is unimodal/convex-like on `(0, l)`:

- If `p` is too close to the first wall, the first jump becomes difficult.
- If `p` is too close to the second wall, the second jump becomes difficult.
- Somewhere in between there is an optimal balance.

Therefore we can use ternary search on `p`.

The final answer is:

```text
sqrt(minimum cost)
```

Because all distances and hole heights are positive, a feasible solution always exists for sufficiently large speed, so `-1` is never needed for the given constraints.

---

## 3. Full solution approach

For each test case:

1. Define a helper function:

```text
jump(d1, d2, b, t)
```

which returns the minimum required speed squared for one jump.

Inside it:

```text
A = g * d1 * d2 / 2
B = g * (d1 + d2)^2 / (2 * d1 * d2)

h0 = d1 * d2 / (d1 + d2)
h = clamp(h0, b, t)

return A / h + B * h
```

2. Define:

```text
cost(p) =
    max(
        jump(ds, p, b1, t1),
        jump(l - p, df, b2, t2)
    )
```

3. Ternary search `p` on interval `[0, l]`.

Although the valid interval is technically `(0, l)`, the minimum is inside the interval, and ternary search never needs to evaluate exactly at endpoints.

4. After enough iterations, compute:

```text
answer = sqrt(cost(best_p))
```

5. Print with sufficient precision, for example 6 digits after the decimal point.

Complexity per test case:

```text
O(iterations)
```

With about `200` or `300` ternary-search iterations, this is constant time and easily fits the time limit.

---

## 4. C++ implementation with detailed comments

```cpp
#include <bits/stdc++.h>
using namespace std;

long double b1, t1, b2, t2, l, ds, df, g;

/*
    Compute the minimum required speed squared for one jump.

    d1 = distance from jump start to the wall
    d2 = distance from wall to landing point
    [b, t] = allowed height interval of the hole

    The frog starts and lands at height 0.
*/
long double minSpeedSquaredForJump(long double d1, long double d2,
                                   long double b, long double t) {
    /*
        For crossing height h, the required speed squared is:

            v^2(h) = A / h + B * h

        where:

            A = g * d1 * d2 / 2
            B = g * (d1 + d2)^2 / (2 * d1 * d2)
    */
    long double A = g * d1 * d2 / 2.0L;

    long double B = g * (d1 + d2) * (d1 + d2)
                    / (2.0L * d1 * d2);

    /*
        The unconstrained optimal crossing height is:

            h0 = d1 * d2 / (d1 + d2)

        But the frog must pass through the hole, so clamp h0 into [b, t].
    */
    long double h0 = d1 * d2 / (d1 + d2);
    long double h = max(b, min(t, h0));

    // Evaluate v^2 at the best allowed height.
    return A / h + B * h;
}

/*
    Solve one test case.
*/
long double solveCase() {
    /*
        cost(p) = required maximum speed squared if the frog lands
        p units after the first wall.
    */
    auto cost = [&](long double p) {
        /*
            First jump:
            - start to first wall: ds
            - first wall to landing point: p
            - hole: [b1, t1]
        */
        long double first = minSpeedSquaredForJump(ds, p, b1, t1);

        /*
            Second jump:
            - landing point to second wall: l - p
            - second wall to final point: df
            - hole: [b2, t2]
        */
        long double second = minSpeedSquaredForJump(l - p, df, b2, t2);

        /*
            The frog has one maximum speed that must work for both jumps.
        */
        return max(first, second);
    };

    /*
        Ternary search for the best landing point p.

        The frog must land between the walls, so 0 < p < l.
        Searching [0, l] is fine because the optimum lies inside,
        and ternary search points are never exactly endpoints.
    */
    long double lo = 0.0L;
    long double hi = l;

    for (int iter = 0; iter < 300; iter++) {
        long double m1 = lo + (hi - lo) / 3.0L;
        long double m2 = hi - (hi - lo) / 3.0L;

        if (cost(m1) < cost(m2)) {
            hi = m2;
        } else {
            lo = m1;
        }
    }

    long double bestP = (lo + hi) / 2.0L;

    /*
        cost(bestP) is speed squared, so take square root.
    */
    return sqrtl(cost(bestP));
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cout << fixed << setprecision(6);

    /*
        Input contains an unknown number of test cases until EOF.
    */
    while (cin >> b1 >> t1 >> b2 >> t2 >> l >> ds >> df >> g) {
        long double answer = solveCase();
        cout << answer << '\n';
    }

    return 0;
}
```

---

## 5. Python implementation with detailed comments

```python
import sys
import math


def min_speed_squared_for_jump(d1, d2, b, t, g):
    """
    Compute the minimum required speed squared for one jump.

    d1:
        distance from jump start to the wall

    d2:
        distance from wall to landing point

    [b, t]:
        allowed hole height interval

    The frog starts and lands at height 0.
    """

    # Coefficients in:
    #
    #     v^2(h) = A / h + B * h
    #
    A = g * d1 * d2 / 2.0
    B = g * (d1 + d2) * (d1 + d2) / (2.0 * d1 * d2)

    # Best crossing height without hole restrictions.
    h0 = d1 * d2 / (d1 + d2)

    # The crossing height must lie inside the hole.
    h = max(b, min(t, h0))

    # Minimum required speed squared for this jump.
    return A / h + B * h


def solve_case(b1, t1, b2, t2, l, ds, df, g):
    """
    Solve one test case and return the minimum required maximum speed.
    """

    def cost(p):
        """
        Required maximum speed squared if the frog lands p units
        after the first wall.
        """

        # First jump:
        # start -> first wall = ds
        # first wall -> landing point = p
        first = min_speed_squared_for_jump(ds, p, b1, t1, g)

        # Second jump:
        # landing point -> second wall = l - p
        # second wall -> final point = df
        second = min_speed_squared_for_jump(l - p, df, b2, t2, g)

        # The maximum speed must be enough for both jumps.
        return max(first, second)

    # Ternary search over the landing point.
    lo = 0.0
    hi = l

    # 200 iterations are more than enough for double precision.
    for _ in range(200):
        m1 = lo + (hi - lo) / 3.0
        m2 = hi - (hi - lo) / 3.0

        if cost(m1) < cost(m2):
            hi = m2
        else:
            lo = m1

    best_p = (lo + hi) / 2.0

    # cost(best_p) is speed squared.
    return math.sqrt(cost(best_p))


def main():
    data = list(map(float, sys.stdin.read().split()))

    answers = []

    # Each test case has exactly 8 real numbers.
    for i in range(0, len(data), 8):
        b1, t1, b2, t2, l, ds, df, g = data[i:i + 8]

        ans = solve_case(b1, t1, b2, t2, l, ds, df, g)

        # 6 digits after decimal are enough for 1e-4 accuracy.
        answers.append(f"{ans:.6f}")

    print("\n".join(answers))


if __name__ == "__main__":
    main()
```