p204.ans1
======================
5.2883
1.3127

=================
p204.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;
}

=================
statement.txt
======================
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

=================
p204.in1
======================
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

=================
