链接 Crossed Ladders
题意
给出两个🏠,在一个房子底部放梯子搭到另一个🏠上,如图
给出梯子长度
x
,
y
x, y
x,y 和它们的交点高度
c
c
c , 求出两个🏠之间的街道宽度;
思路
首先很容易想到利用三角形相似;
三角形相似得出两个式子
w
−
a
w
=
c
x
2
−
w
2
\frac{w -a}{w} = \frac{c}{\sqrt{x^2-w^2}}
ww−a=x2−w2c
a
w
=
c
y
2
−
w
2
\frac{a}{w} = \frac{c}{\sqrt{y^2-w^2}}
wa=y2−w2c
整理一下得
c y 2 − w 2 + c x 2 − w 2 − 1 = 0 \frac{c}{\sqrt{y^2-w^2}} + \frac{c}{\sqrt{x^2-w^2}} - 1=0 y2−w2c+x2−w2c−1=0
分析可得这个关于 w w w 的方程是单调的,所以我们对 w w w 进行二分;
AC代码
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <map>
#include <vector>
#include <string>
#define x first
#define y second
using namespace std;
const double eps = 1e-12;
const int N = 210;
typedef pair<double, double> PDD;
const double pi = acos(-1);
int T;
double x, y, c;
bool check(double u)
{
return c / sqrt(y * y - u * u) + c / sqrt(x * x - u * u) - 1 >= 0;
}
int main()
{
scanf("%d", &T);
for (int i = 1; i <= T; i ++)
{
scanf("%lf%lf%lf", &x, &y, &c);
double l = 0, r = min(x, y);
while (r - l > eps)
{
double mid = (l + r) / 2.0;
if (check(mid)) r = mid;
else l = mid;
}
printf("Case %d: %.10lf\n", i, r);
}
}