[清华集训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$时,人在墙上的影子 ...
随机推荐
- Sping注解开发
基本注解 @Configuration 作用: 标记在类上表示是一个配置类(相当于一个配置类) @Bean 作用: 在容器中放一个bean相当于xml文件里的bean标签 @Configuration ...
- SpringBoot+Jpa+SpringSecurity+Redis+Vue的前后端分离开源系统
项目简介: eladmin基于 Spring Boot 2.1.0 . Jpa. Spring Security.redis.Vue的前后端分离的后台管理系统,项目采用分模块开发方式, 权限控制采用 ...
- ML- 核函数(Kernel) 的 SVM
Why 核函数 目的是为了解决线性不可分问题. 核心思想是升维. 当样本点在低维空间不能很好地分开的时候, 可以考虑将样本通过某种映射(就是左乘一个矩阵) 到高维空间中, 然后在高维空间就容易求解一个 ...
- Django 初识之安装下载以及模型目录简介
Django 一.web应用 web应用什么 Web应用程序是一种可以通过Web访问的应用程序,程序的最大好处是用户很容易访问应用程序,用户只需要有浏览器即可,不需要再安装其他软件 应用程序有两种模式 ...
- 带你快速上手前端三剑客之css
CSS介绍 CSS(Cascading Style Sheet , 层叠样式表)定义如何显示HTML元素.当浏览器读到一个样式表,它就会按照这个样式表来对文档进行格式化(渲染) 组成 每个CS ...
- linux命令实现在当前文件夹下面模糊搜索文件
在当前文件中查找包含的字符串 find . -name "*.txt" | xargs grep 'abc' ,例如:查找txt文件中包含字符串a的字符串
- 微服务框架---搭建 go-micro环境
1.安装micro 需要使用GO1.11以上版本 #linux 下 export GO111MODULE=on export GOPROXY=https://goproxy.io # windows下 ...
- List 分隔多次执行 且在同一个事物当中
@Override public boolean insrtListDatas(List<EtlCheckrecord> listmap, int fen) { if(fen<=0) ...
- 推荐系统(recommender systems):预测电影评分--问题描述
推荐系统很重要的原因:1>它是机器学习的一个重要应用2>对于机器学习来说,特征是非常重要的,对于一些问题,存在一些算法能自动帮我选择一些优良的features,推荐系统就可以帮助我们做这样 ...
- sql 查询哪些字段重复及(in和exict的区别)
select count(1),content_id,keyword_id from tb_content_keyword_relation group by content_id,keyword_i ...