The problem is as shown in the figure. Given x
, y
, c
, find d
.
We can see that; given the distance between the two buildings d
and the lengths of the ladders x
& y
, we can determisiticly find the hight of the intersection c
.
However, we’re given x
, y
, c
and required to find d
. Thus, a binary search for d
will do.
- We know that
0 <= d <= min(x,y)
- Given
(x, y, d)
, we can findc'
. - If
c'
>c
, we need mored
. And vice versa.
#include <iostream> #include <complex> #include <limits> using namespace std; typedef double gtype; #define equ(a, b, e) ( abs((a) - (b)) <= (e) ) const gtype epsLen = 10E-6; typedef complex<gtype> point; #define x real() #define y imag() #define crs(a, b) ( (conj(a) * (b)).y ) #define intrN(a, b, p, q) ( crs((p)-(a),(b)-(a)) * (q) - crs((q)-(a),(b)-(a)) * (p) ) #define intrD(a, b, p, q) ( crs((p)-(a),(b)-(a)) - crs((q)-(a),(b)-(a)) ) #define cosL(a, b, c) ( ((a) * (a) + (b) * (b) - (c) * (c)) / (2 * (a) * (b)) ) gtype getD(gtype x0, gtype y0, gtype d) { gtype y1, x1, intrDen; y1 = sqrt(y0 * y0 - d * d); x1 = sqrt(x0 * x0 - d * d); intrDen = intrD(point(0,0), point(d,x1), point(0,y1), point(d,0)); if (equ(intrDen, 0, epsLen)) return 0; return (intrN(point(0,0), point(d,x1), point(0,y1), point(d,0)) / intrDen).y; } int main() { cout.precision(3); gtype x0, y0, c0; while (cin >> x0 >> y0 >> c0) { gtype lD = 0, hD = min(x0, y0) + epsLen, mD; do { mD = (hD + lD) / 2.0; if (getD(x0, y0, mD) < c0) hD = mD; else lD = mD; } while (hD - lD > epsLen); cout << fixed << mD << endl; } return 0; }