ZOJ_3203_LightBulb

来源:互联网 发布:自动化设备 图形 编程 编辑:程序博客网 时间:2024/06/02 06:31

Light Bulb

Time Limit: 1 Second      Memory Limit: 32768 KB

Compared to wildleopard's wealthiness, his brother mildleopard is rather poor. His house is narrow and he has only one light bulb in his house. Every night, he is wandering in his incommodious house, thinking of how to earn more money. One day, he found that the length of his shadow was changing from time to time while walking between the light bulb and the wall of his house. A sudden thought ran through his mind and he wanted to know the maximum length of his shadow.

Input

The first line of the input contains an integer T (T <= 100), indicating the number of cases.

Each test case contains three real numbers Hh and D in one line. H is the height of the light bulb while h is the height of mildleopard. D is distance between the light bulb and the wall. All numbers are in range from 10-2 to 103, both inclusive, and H - h >= 10-2.

Output

For each test case, output the maximum length of mildleopard's shadow in one line, accurate up to three decimal places..

Sample Input

32 1 0.52 0.5 34 3 4

Sample Output

1.0000.7504.000


Author: GUAN, Yao
Source: The 6th Zhejiang Provincial Collegiate Programming Contest


题目意思求如图所示的影子最大长度

显然当影子到达墙壁弯折之前的任意状态长度不如刚好到达墙壁时

影子到达墙壁后 墙壁上的影子变长

而地上的影子变短

因此影子长度应该为一个凸函数,三分求最大值就可以了

这里选取了人与灯的水平距离x为变量


这个题目精度要设置的高些 1e-8WA了一发……

#include <iostream>#include <stdio.h>#include <math.h>using namespace std;const double AC=1e-9;double H,h,D;double f(double x){    double s1=H*x/(H-h);    double s2=(s1-D)*H/s1;    return D-x+s2;}double ts(){    double lo=(H-h)*D/H,hi=D;    double mid,midmid;    while(hi-lo>AC)    {        mid=(lo+hi)/2;        midmid=(mid+hi)/2;        //cout<<"ts "<<mid<<" "<<midmid<<endl;        if(f(mid)>f(midmid))            hi=midmid;        else            lo=mid;        //cout<<endl;    }    return f(lo);}int main(){    int t;    scanf("%d",&t);    while(t--)    {        scanf("%lf%lf%lf",&H,&h,&D);        printf("%.3lf\n",ts());    }    return 0;}


0 0