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. 【BZOJ】【2434】【NOI2011】阿狸的打字机

    AC自动机+DFS序+BIT 好题啊……orz PoPoQQQ 大爷 一道相似的题目:[BZOJ][3172][TJOI2013]单词 那道题也是在fail树上数有多少个点,只不过这题是在x的fail ...

  2. Singleton 单例模式 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  3. [Vue warn]: Error in render: "TypeError: Cannot read property '0' of undefined、vuejs路由使用的问题Error in render function

    1.[Vue warn]: Error in render: "TypeError: Cannot read property '0' of undefined 注意,只要出现Error i ...

  4. 数据库操作语句类型(DQL、DML、DDL、DCL)简介

    SQL语言共分为四大类:数据查询语言DQL,数据操纵语言DML,数据定义语言DDL,数据控制语言DCL. 1. 数据查询语言DQL数据查询语言DQL基本结构是由SELECT子句,FROM子句,WHER ...

  5. python 插入数据获取id

    python 插入数据获取id 学习了:https://blog.csdn.net/qq_37788558/article/details/78151972 commit之前获取 cursor.las ...

  6. Visual Studio环境变量使用实例:使用环境变量来组织project

    前言 在前一篇文章Visual Studio中的环境变量(以Visual Studio 2013为例)中介绍了VS中的环境变量,本文将以实际样例说明怎样合理使用这些环境变量来组织VC++project ...

  7. Android直播实现 Android端推流、播放

    最近想实现一个Android直播,但是对于这方面的资料都比较零碎,一开始是打算用ffmpeg来实现编码推流,在搜集资料期间,找到了几个强大的开源库,直接避免了jni的代码,集成后只用少量的java代码 ...

  8. 创建线程安全的单例(ARC或 非ARC)

    一:创建 宏 文件 SynthesizeSingleton.h SynthesizeSingleton.h #if __has_feature(objc_arc) // ARC Version #de ...

  9. Yahoo团队总结的关于网站性能优化的经验(转)

    英文原文:http://developer.yahoo.com/performance/rules.html 中文原文:http://www.ha97.com/2710.html 1.尽量减少HTTP ...

  10. 借助AWR报告分析解决oracleCPU过高的问题(转)

    原文地址:http://www.cnblogs.com/crystal-guoguo/p/4213458.html 简介:在oracle数据库中,有两个非常实用的自带监控工具EM(Enterprise ...