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.
Input file contains at most 1000 test cases.
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 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
Sample Output
5.2883
1.3127
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-10;
double b1, t1, b2, t2, l, ds, df, g;
//prove: know l, when aofa = pi / 2, v is smallest
//konw g, set vx, vy, t
//l = vx * t, vy = g * t / 2
//t = l / vx = 2 * vy / g
//vx * vy = l * g / 2
//so v = sqrt(vx * vx + vy * vy) >= sqrt(2 * vx * vy) = sqrt(g * l)
//so when vx = vy, v get the smallest value, then aofa = pi / 2
//
//calculate: know l, aofa = pi / 2, when the point at s, cal h
//as above, vx * vy = g * l / 2, and vx = vy
//so vx = sqrt(g * l / 2), then v = sqrt(g * l)
//set now time is t
//then t = s / vx
//as konw, h = vy * t - g * t * t / 2
// = vy * s / vx - g * s * s / (2 * vx * vx)
// = s - s * s / l
//
//however, sometime it is not satisfied b <= h <= t
//while h < b
//as we know it should pass point(s, b)
//so let t0 for the time pass point(s, b)
//then vx * t0 = s and vy * t0 - g * t0 * t0 / 2 = b
//so b = vy * s / vx - g * s * s / vx / vx / 2
//as above, vx * vy = g * l
// so vx * vx = g * s / 2 / b * (l - s)
// so vy * vy = b * g * l * l / 2 / s / (l - s)
// so v = sqrt(vx * vx + vy * vy)
// = sqrt(g * s * (l - s) / 2 / b + g * l * l * b / (2 * s * (l - s)))
//
//while h > t, the same as above
//so v = sqrt(g * s * (l - s) / 2 / t + g * l * l * t / (2 * s * (l - s)))
//
//that's all
double get(double l, double s, double b, double t){
double h = s - s * s / l;
if(h < b){
return sqrt(g * s * (l - s) / 2 / b + g * l * l * b / (2 * s * (l - s)));
}
else if(h > t){
return sqrt(g * s * (l - s) / 2 / t + g * l * l * t / (2 * s * (l - s)));
}
else{
return sqrt(g * l);
}
}
double cal(double s){
double temp = get(ds + s, ds, b1, t1);
temp = max(temp, get(df + l - s, df, b2, t2));
return temp;
}
int main(){
freopen("in.txt", "r", stdin);
while(cin >> b1 >> t1 >> b2 >> t2 >> l >> ds >> df >> g){
double left = 0, right = l;
int i = 0;
while(right - left > eps || i++ < 88){
//for(int i = 0; i < 100; i++){
double mid1 = left + (right - left) / 3;
double mid2 = right - (right - left) / 3;
if(cal(mid1) < cal(mid2)) right = mid2;
else left = mid1;
}
printf("%.6f\n", cal(left));
}
return 0;
}