[LightOJ] [1062] Crossed Ladders

Problem Description

Loading...

Problem Analysis

From the problem we can write an equation relating the height c and the gap d

$\dfrac{c }{\sqrt{x^2 - d^2}} + \dfrac{c }{\sqrt{y^2 - d^2}} = 1$

which simplifies to

$c =\dfrac{1}{ \dfrac{1 }{\sqrt{x^2 - d^2}} + \dfrac{1 }{\sqrt{y^2 - d^2}}}$

It follows that if d is larger than res
then c should be smaller than the given c
so we can compare mid with res
and thus perform a Binary Search

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<stdio.h>
#include<math.h>
double x,y,c;
bool judge(double m) {
	double res=1.0/(1.0/sqrt(y*y-m*m)+1.0/sqrt(x*x-m*m));
	if(res>c)
		return true;
	else
		return false;
}
int main() {
	int T,kase=0;
	scanf("%d",&T);
	while(T--) {
		scanf("%lf %lf %lf",&x,&y,&c);
		double l=0,r=(x<y?x:y);
		while(r-l>1e-8) {
			double mid=(l+r)/2;
			if(judge(mid))
				l=mid;
			else
				r=mid;
		}
		printf("Case %d: %.8lf\n",++kase,l);
	}
	return 0;
}
This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...