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. Redis五大数据结构

    1.Redis介绍 Redis是REmote DIctionary Server的缩写,作者定位于一个内存KV存储数据库(In-memory key-value Store),让Redis自豪的并不是 ...

  2. SXH232摄像头使用示范

    It occurred to me suddenly that I wanted to program the our camera sensor for PC desktop, just like ...

  3. Hadoop目录

    1. 通过java读取HDFS的数据 (转) 2. FLume监控文件夹,将数据发送给Kafka以及HDFS的配置文件详解 3. 开启hadoop和Hbase集群的lzo压缩功能(转) 4. Hado ...

  4. ubuntu下mongodb常用命令

    1. 启动脚本 #!/bin/bash mongod --dbpath /usr/local/mongodb/data1 chmod +x run-mongodb 2. 关闭数据库服务 官方文档说可以 ...

  5. JDBC 通过PreparedStatement 对数据库进行增删改查

    1 插入数据 public boolean ChaRu3(User user){ boolean flag=true; Connection conn=null; PreparedStatement ...

  6. 什么是Copy-Only Backup? 为什么要用它?

    Copy-only backup是一种独立于传统SQL Backup方法的一种备份方式. 一般来说, 做一次数据库备份会影响到后面的备份和还原作业. 然而, 有时你需要为了某个特殊的目的而做一次备份但 ...

  7. CSS写的提示框(兼容火狐IE等各大浏览器)

    项目上使用jQuery的Tooltip组件,在谷歌上正常,在火狐和IE下没有效果,所以根据谷歌的提示框单独用CSS写了个提示框,比较好的兼容了火狐和IE,且效果一样 原Tooltip代码: $('#d ...

  8. SQL基础(五):SQL函数

    一.SQL Aggregate 函数: SQL Aggregate 函数计算从列中取得的值,返回一个单一的值. 1.AVG() 函数 AVG() 函数返回数值列的平均值. 语法:SELECT AVG( ...

  9. 编写批处理文件编译.Net工程

    使用随Visual Studio一块安装的devenv.com,再加上参数可以对.Net进行编译,如下 "D:\Program Files\Microsoft Visual Studio 8 ...

  10. Android menu+ anctionbar

    一.概述 Menu,简单来理解就是当你按下手机的"menu"键时所弹出来的窗体,它被广泛应用着,差点儿在每一个应用中都有它的身影. 二.要求 用两种方式实现菜单功能. 三.实现 新 ...