The tiangles and are similar: \frac{c}{b} = \frac{AP}{AC}
The tiangles and are also similar: \frac{c}{a} = \frac{CP}{CA}
Whence
We’ll find the width of the street d = AC using the binary search method.
Initially, let 0 \le d \le min(x, y). Given the values of and , we can compute and . For fixed and as increases, the value of decreases.
Algorithm implementation
Read the input data for each test case.
while(scanf("%lf %lf %lf",&x,&y,&c) == 3)
{Set the initial values: left = 0, right = min(x,y). During the execution of the loop, the inequality left \le d \le right.
left = 0;
if (x < y) right = x; else right = y;
d = (left + right) / 2;
do
{Compute the values of a, b, c.
a = sqrt(x*x - d*d); b = sqrt(y*y - d*d);
c1 = 1/(1/a + 1/b);If the computed value is less than the given , the upper bound of should be decreased. Otherwise, the lower bound should be increased.
if (c1 < c) right = (left + right) / 2;
else left = (left + right) / 2;
d = (left + right) / 2;The computations are performed until the required accuracy specified in the problem statement is reached — four decimal places.
} while (fabs(c1 - c) > 0.00001);Print the answer.
printf("%.3lf\n",d);
}Java implementation
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner con = new Scanner(System.in);
con.useLocale(new Locale("US"));
while(con.hasNext())
{
double x = con.nextDouble();
double y = con.nextDouble();
double c = con.nextDouble();
double Left = 0, Right, a, b, c1;
if (x < y) Right = x; else Right = y;
double d = (Left + Right) / 2;
do
{
a = Math.sqrt(x*x - d*d);
b = Math.sqrt(y*y - d*d);
c1 = 1/(1/a + 1/b);
if (c1 < c) Right = (Left + Right) / 2;
else Left = (Left + Right) / 2;
d = (Left + Right) / 2;
} while (Math.abs(c1 - c) > 0.00001);
System.out.format(Locale.US,"%.3f\n",d);
}
}
}