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

```cpp
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

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

bool read() {
    cin >> b1 >> t1 >> b2 >> t2 >> l >> ds >> df >> g;
    return !cin.fail();
}

void solve() {
    // The free variables are the landing point p in (0, l) between the
    // walls and the two hole-crossing heights h_1 in [b1, t1] and h_2 in
    // [b2, t2]. The jumper has a single max-speed v that must cover both
    // jumps, so for a given p the smallest feasible v is max(v1(p), v2(p))
    // with v_i(p) the minimum speed for the i-th jump. For a fixed p the
    // two jumps are independent and we minimise each v_i over its h_i in
    // closed form; that leaves p, which couples the jumps (moving p right
    // lengthens jump 1 and shortens jump 2) and we handle by ternary
    // searching
    //
    //     min over p of max(v1(p), v2(p)).
    //
    // Consider a jump from (0, 0) over the hole at (d1, h) to (d1 + d2, 0).
    // We pick coordinates so launch and landing both have y = 0, so the
    // trajectory is a parabola with roots at x = 0 and x = R := d1 + d2.
    // Any such parabola has the form
    //
    //     y(x) = a * x * (R - x),
    //
    // which already bakes in the two ground crossings; only the single
    // shape parameter a is left to fix. We fix it from the hole point:
    // y(d1) = a * d1 * d2 = h, so a = h / (d1 * d2).
    //
    // To turn this geometric parabola into a physical trajectory, write
    // the projectile motion x(t) = v_x * t, y(t) = v_y * t - g * t^2 / 2,
    // eliminate t via t = x / v_x:
    //
    //     y(x) = (v_y / v_x) * x  -  (g / (2 * v_x^2)) * x^2.
    //
    // Matching the x^2 coefficient with a * x * (R - x) = a * R * x - a * x^2
    // gives a = g / (2 * v_x^2), i.e. v_x^2 = g / (2 * a). And matching the
    // linear coefficient gives v_y / v_x = a * R, i.e. tan(theta) = a * R.
    // Then
    //
    //     v^2 = v_x^2 + v_y^2 = v_x^2 * (1 + tan^2 theta)
    //         = g / (2 * a) + g * a * R^2 / 2
    //         = A / h + B * h,
    //
    //     A = g * d1 * d2 / 2,   B = g * (d1 + d2)^2 / (2 * d1 * d2).
    //
    // f(h) = A / h + B * h is convex on h > 0 with unconstrained minimum
    // at h_star = sqrt(A / B) = d1 * d2 / (d1 + d2) and minimum value
    // g * (d1 + d2). The hole forces h in [b, t], so the optimal h is
    // clamp(h_star, b, t) and v_i(p)^2 = f at that h.
    //
    // Each v_i(p) is U-shaped in p (constraint-bound for extreme p,
    // unconstrained = g * (d1 + d2) in the middle), and v1, v2 move in
    // opposite directions with p, so max(v1, v2) is unimodal and ternary
    // search on p in (0, l) finds the optimum. -1 is never needed: every
    // jump is feasible for sufficiently large v since b > 0.

    auto min_v2_jump = [](long double d1, long double d2, long double b,
                          long double t) {
        long double A = g * d1 * d2 / 2.0L;
        long double B = g * (d1 + d2) * (d1 + d2) / (2.0L * d1 * d2);
        long double h = max(b, min(t, d1 * d2 / (d1 + d2)));
        return A / h + B * h;
    };

    auto cost = [&](long double p) {
        return max(min_v2_jump(ds, p, b1, t1), min_v2_jump(l - p, df, b2, t2));
    };

    long double lo = 0.0L, 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;
        }
    }

    cout << sqrtl(cost((lo + hi) / 2.0L)) << '\n';
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(6);

    while(read()) {
        solve();
    }

    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()
```