[清华集训2015]灯泡(浙江大学ZOJ 3203 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 H, h 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
题目大意:
他的房子狭窄,他家里只有一个灯泡。每天晚上,他都在他不知名的房子里徘徊,想着如何赚更多的钱。有一天,他发现他的影子长度在灯泡和房子的墙壁之间行走时不时变化。突然想到了他的思绪,他想知道他的影子的最大长度。
输入的第一行包含整数T(T <= 100),表示个案数。
每个测试用例在一行中 包含三个实数H,h和D. H是灯泡的高度,而h是轻度高度的高度。 D是灯泡和墙壁之间的距离。所有数字的范围均为10 -2至10 3,包括两者,H - h > = 10 -2。
对于每个测试用例,在一行中输出mildleopard阴影的最大长度,精确到三位小数。
算法分析
参考:
https://blog.csdn.net/Mrx_Nh/article/details/52745348 分析理解应该是对的,但是代码有误,估计是没有讨论影子不落到墙上的情况。
https://blog.csdn.net/qq_38944163/article/details/80870928 代码是正确的。
这个题要分两种情况讨论:影子没有落到墙上;影子落到墙上。
影子没有落到墙上的情况如下图所示:

影子落到墙上的情况如下图所示

算影子的长度可以用相似三角形,相信各位上过初中的朋友都会。。
那么我们发现这是一个单峰函数,要求峰值,这里我们可以用一个很强大的东西,三分法。
具体看代码吧
#include<stdio.h>
double f(double x,double H,double h,double D)
{
double tanA,Y;
tanA=(H-h)/x;//这里大家可以画个图帮助理解
Y=H/tanA;//下面那条的长(在没有墙的情况下)
if(Y<=D) return Y-x;//如果没到墙,就直接返回地上影子的长度。
Y=Y-D;
return D-x+Y*tanA;//D-X是地上影子的长度,Y*tanA是墙上影子的长度.
}
int main()
{
freopen("p3382.in","r",stdin);
int i,T;
double H,h,D;
double L,R,m1,m2,fm1,fm2;
scanf("%d",&T);
for(i=;i<T;i++)
{
scanf("%lf%lf%lf",&H,&h,&D);
L=;R=D;
while(L+1e-<R)
{
m1=L+(R-L)/; m2=R-(R-L)/;
fm1=f(m1,H,h,D); fm2=f(m2,H,h,D);
if(fm1<fm2) L=m1;
else R=m2;
}
printf("%.3lf\n",f(L,H,h,D));
}
return ;
}
为何是一个单峰,可以考虑看一下第一个参考的博客。

理解时参考下图:

[清华集训2015]灯泡(浙江大学ZOJ 3203 Light Bulb)的更多相关文章
- ZOJ 3203 Light Bulb (三分+计算几何)
B - Light Bulb Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit ...
- 三分 --- ZOJ 3203 Light Bulb
Light Bulb Problem's Link: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3203 Mean: ...
- ZOJ 3203 Light Bulb - 求导求最大值
如果L全在地面上: 输出 h * D / H 如果L全在墙上: 输出 h 否则: (D - X ) / X = Y / (H - h) L = D - X + h - Y 然后对L求导即可 #incl ...
- zoj 3203 Light Bulb,三分之二的基本问题
Light Bulb Time Limit: 1 Second Memory Limit: 32768 KB Compared to wildleopard's wealthiness, h ...
- ZOJ 3203 Light Bulb (三分查找)
Light Bulb Time Limit: 1 Second Memory Limit: 32768 KB Compared to wildleopard's wealthiness, h ...
- ZOJ 3203 Light Bulb
Compared to wildleopard's wealthiness, his brother mildleopard is rather poor. His house is narrow a ...
- ZOJ 3203 Light Bulb(数学对勾函数)
Light Bulb Time Limit: 1 Second Memory Limit: 32768 KB Compared to wildleopard's wealthiness, h ...
- ZOJ 3203 Light Bulb( 三分求极值 )
链接:传送门 题意: 求影子长度 L 的最大值 思路:如果 x = 0 ,即影子到达右下角时,如果人继续向后走,那么影子一定是缩短的,所以不考虑这种情况.根据图中的辅助线外加相似三角形定理可以得到 L ...
- ZOJ - 3203 Light Bulb(三分)
题意:灯离地面的高度为$H$,人的身高为$h$,灯离墙的距离为$D$,人站在不同位置,影子的长度不一样,求出影子的最长长度. 思路:设人离灯的距离为$x$,当人走到距离灯长度为$L$时,人在墙上的影子 ...
随机推荐
- SAP技术 - How to create a CDS redirect view for a given database table
Scenario Suppose we have a database table A, and then we create a CDS redirect view B for it, then e ...
- 无法读取例程 &ROUTINE 中配置文件选项 FND_DEVELOPER_MODE
问题描述:OM>发运>事务处理,进入此界面,FORM出现报错信息:无法读取例程 &ROUTINE 中配置文件选项 FND_DEVELOPER_MODE 解决办法:在系统管理员下,设 ...
- Nginx + PHP 修改单次请求 最大执行时间
1. php.ini (usr/local/php/etc) max_execution_time = 2. php-fpm.conf (usr/local/php/etc) request_t ...
- Oracle数据库主外键 级联删除记录
/** * 1. NO ACTION :指当删除主表中被引用列的数据时,如果子表的引用列中包含该值,则禁止该操作执行. * * 2. SET NULL :指当删除主表中被引用列的数据时,将子表中相应引 ...
- SAMBA 服务配置
Samba文件共享服务 Linux系统中一种文件共享程序 在Windows网络环境中,主机之间进行文件和打印机共享是通过微软公司自己的SMB/CIFS网络协议实现的.SMB(Server Messag ...
- 实验十四:团队项目评审&课程学习总结
项目 内容 作业所属课程 所属课程 作业要求 作业要求 课程学习目标 (1)掌握软件项目评审会流程:(2)反思总结课程学习内容 任务一:团队项目审核已完成.项目验收过程意见表已上交. 任务二:课程学习 ...
- 软件工程1916|W(福州大学)_助教博客】助教总结
2018年上个学期,我担任计算机专业软件工程实践课程的助教,因为有汪老师和她的两位优秀研究生担任助教,基本上没有让我做什么事,除了参加了几次课程答辩.我对于助教需要做的事情其实还是没什么概念的.这个学 ...
- HDU - 6643: Ridiculous Netizens(点分治+依赖背包+空间优化)
题意:给定带点权的树,问多少个连通块,其乘积<=M; N<=2000,M<1e6; 思路:连通块-->分治: 由于普通的树DP在合并的时候复杂度会高一个M,所以用依赖背包来做. ...
- scala简单的功能实现~weektwo
1.编写⼀个BankAccount类,假如deposit和withdraw⽅法,和⼀个只读的balance属性. //存款(deposit)和取款()函数 class BankAccount exte ...
- Go语言 - 接口
接口类型 在Go语言中接口(interface)是一种类型,一种抽象的类型. interface是一组method的集合,是duck-type programming的一种体现.接口做的事情就像是定义 ...