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

3
2 1 0.5
2 0.5 3
4 3 4

Sample Output

1.000
0.750
4.000

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

二分法作为分治中最常见的方法,适用于单调函数,逼近求解某点的值。但当函数是凸性函数时,二分法就无法适用,这时三分法就可以“大显身手”~

如图,类似二分的定义Left和Right,mid = (Left + Right) / 2,midmid = (mid + Right) / 2;

如果mid靠近极值点,则Right = midmid;否则(即midmid靠近极值点),则Left = mid;

模版如下:

int Cale(int  ){

}

int Solve(int left ,int right){
int mid,midmid;
while(left<right){
mid=(left+right)/;
midmid=(mid+right)/;
if(Cale(mid) > Cale(midmid))
right=midmid;///////假设求最大值
else
left=mid;
}
return left;
}

如图,人左右走动,求影子L的最长长度。根据图,很容易发现当灯,人的头部和墙角成一条直线时(假设此时人站在A点),此时的长度是影子全在地上的最长长度。当人再向右走时,影子开始投影到墙上,当人贴着墙,影子长度即为人的高度。所以当人从A点走到墙,函数是先递增再递减,为凸性函数,所以我们可以用  三分法 来求解。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath> using namespace std; const double eps=1e-; double H,h,D,ans; double Cal(double l){
return l+D*(h-l)/(H-l);
} int main(){ //freopen("input.txt","r",stdin); int t;
scanf("%d",&t);
while(t--){
scanf("%lf%lf%lf",&H,&h,&D);
double left=,right=h,mid,midmid;
while(right-left>=eps){
mid=(left+right)/;
midmid=(mid+right)/;
if(Cal(mid)>Cal(midmid))
right=midmid;
else
left=mid;
}
printf("%.3lf\n",Cal(left));
}
return ;
}

ZOJ 3203 Light Bulb (三分查找)的更多相关文章

  1. ZOJ 3203 Light Bulb (三分+计算几何)

    B - Light Bulb Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit ...

  2. ZOJ - 3203 Light Bulb(三分)

    题意:灯离地面的高度为$H$,人的身高为$h$,灯离墙的距离为$D$,人站在不同位置,影子的长度不一样,求出影子的最长长度. 思路:设人离灯的距离为$x$,当人走到距离灯长度为$L$时,人在墙上的影子 ...

  3. 三分 --- ZOJ 3203 Light Bulb

    Light Bulb Problem's Link:   http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3203 Mean: ...

  4. ZOJ 3203 Light Bulb - 求导求最大值

    如果L全在地面上: 输出 h * D / H 如果L全在墙上: 输出 h 否则: (D - X ) / X = Y / (H - h) L = D - X + h - Y 然后对L求导即可 #incl ...

  5. zoj 3203 Light Bulb,三分之二的基本问题

    Light Bulb Time Limit: 1 Second      Memory Limit: 32768 KB Compared to wildleopard's wealthiness, h ...

  6. ZOJ 3203 Light Bulb

    Compared to wildleopard's wealthiness, his brother mildleopard is rather poor. His house is narrow a ...

  7. ZOJ 3203 Light Bulb(数学对勾函数)

    Light Bulb Time Limit: 1 Second      Memory Limit: 32768 KB Compared to wildleopard's wealthiness, h ...

  8. [清华集训2015]灯泡(浙江大学ZOJ 3203 Light Bulb)

    Time Limit: 1 Second      Memory Limit: 32768 KB Compared to wildleopard's wealthiness, his brother ...

  9. ZOJ 3203 Light Bulb( 三分求极值 )

    链接:传送门 题意: 求影子长度 L 的最大值 思路:如果 x = 0 ,即影子到达右下角时,如果人继续向后走,那么影子一定是缩短的,所以不考虑这种情况.根据图中的辅助线外加相似三角形定理可以得到 L ...

随机推荐

  1. java静态代码分析工具infer

    infer是一个静态代码分析工具,探测bugs. 主要支持Java.C/C++ 安装:brew install infer 在线展示:https://codeboard.io/projects/115 ...

  2. 【BestCoder】【Round#41】

    枚举+组合数?+DP+数学问题 http://bestcoder.hdu.edu.cn/contests/contest_show.php?cid=582 QAQ许久没打过比赛,来一发BC,结果还是只 ...

  3. SQL Server基础知识三十三问 (15-21)

    15. 存储过程可以调用自己么, 或者说可能有递归的存储过程么? SP nesting最多可以到多少层? 答: 可以的. 因为Transact-SQL 支持递归, 你可以编写可以调用自己的存储过程. ...

  4. DIV-CSS布局中position属性详解

    本文向大家描述一下DIV CSS布局中的position属性的用法,position属性主要有四种属性值,任何元素的默认position的属性值均是static,静态.这节课主要讲讲relative( ...

  5. Tomcat gzip果然强大,js文件压缩率50%以上

    Tomcat配置使用gzip,在server.xml中 <Connector port="9098" protocol="HTTP/1.1" connec ...

  6. 转: Eclipse设置、调优、使用 (强烈推荐)

    转注:写的非常详累,并且图文并茂,值得一看. http://yuanzhifei89.iteye.com/blog/974082 eclipse调优 一般在不对eclipse进行相关设置的时候,使用e ...

  7. Linux随笔(安装ftp,安装jdk,安装 tomcat,安装redis,安装MySQL,安装svn)

    su: authentication failure 解决办法:sudo passwd root  更改密码即可 确认虚拟机用到的联网方式是桥接模式,不然Windows是ping不通Linux的,确保 ...

  8. (LeetCode 153)Find Minimum in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  9. SICP-练习2.17

    [问题] 请定义出过程last-pair.它返回仅仅包括给定(非空)表里最后一个元素的表: (last-pair (list 23 72 149 34)) (34) [分析] last-pair须要处 ...

  10. C#.NET常见问题(FAQ)-如何把写好的类编译成dll文件

    1 新建一个类库项目   2 直接改写这个Class1.cs文件   3 记得要添加Windows.Forms引用   4 我直接把在别的项目中做好的cs文件搞到这里来,连文件名也改了(FilesDi ...