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

题目链接

题目大意:

他的房子狭窄,他家里只有一个灯泡。每天晚上,他都在他不知名的房子里徘徊,想着如何赚更多的钱。有一天,他发现他的影子长度在灯泡和房子的墙壁之间行走时不时变化。突然想到了他的思绪,他想知道他的影子的最大长度。

输入的第一行包含整数TT <= 100),表示个案数。

每个测试用例在一行中 包含三个实数HhD. 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)的更多相关文章

  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

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

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

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

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

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

  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. ZOJ 3203 Light Bulb( 三分求极值 )

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

  9. ZOJ - 3203 Light Bulb(三分)

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

随机推荐

  1. SpringBoot2.x的Maven依赖配置

    本篇主要说明以下内容: 1.SpringBoot2.x中Maven的配置内容,即:pom.xml的内容说明 1 Maven依赖的配置方式 使用Maven来配置SpringBoot2.x,有两种方式: ...

  2. No root/virtual joint specified in SRDF. Assuming fixed joint

    在用MoveIt!配置文件时,加载urdf模型时,显示Success......但没有显示模型,终端显示错误如下: 增加虚拟关节就好.

  3. M × N Puzzle

    http://poj.org/problem?id=2893 来自逆序对的强大力量 #include<iostream> #include<stdio.h> #include& ...

  4. Css背景设置 、

    每天进步一小步,一年进步一大步. 第一次发博客园文章,主要记录自己学习的一个过程. CSS3 背景 CSS3 包含多个新的背景属性,它们提供了对背景更强大的控制. background-size ba ...

  5. MapTask工作机制

    (1)Read阶段:MapTask通过用户编写的RecordReader,从输入InputSplit中解析出一个个key/value. (2)Map阶段:该节点主要是将解析出的key/value交给用 ...

  6. MySQL利用LOCATE,CONCAT查询

    SELECT * FROM table t WHERE 1=1 AND LOCATE(#{searchParams},CONCAT(t.account,IFNULL(t.id,''),IFNULL(t ...

  7. Socket内核调用数SYSCALL_DEFINE3

    http://blog.chinaunix.net/uid-20788636-id-4408261.html 前言: 对于Linux内核的Socket系列文章都是依据于:Linux-3.14.5的版本 ...

  8. (尚032)Vue_案例_消息订阅与发布(组件订阅与发布(PubSubJS库))

    组件订阅与发布(PubSubJS库) npm info pubsub-js 安装库 npm install --save pubsub -js ============================ ...

  9. Linux后台开发工具箱-葵花宝典

    Linux后台开发工具箱-葵花宝典 一见 2016/11/4 目录 目录 1 1. 前言 4 2. 脚本类工具 4 2.1. 双引号和单引号 4 2.2. 取脚本完整文件路径 5 2.3. 环境变量和 ...

  10. NOI2019 回家路线 DP

    「NOI2019」回家路线 链接 loj 思路 f[i][j]第i个点,时间为j,暴力转移 复杂度O(m*t),好像正解是斜率优化,出题人太不小心了233 代码 #include <bits/s ...