[清华集训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 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  代码是正确的。

这个题要分两种情况讨论:影子没有落到墙上;影子落到墙上。

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

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

算影子的长度可以用相似三角形,相信各位上过初中的朋友都会。。

那么我们发现这是一个单峰函数,要求峰值,这里我们可以用一个很强大的东西,三分法。

具体看代码吧

 1 #include<stdio.h>
 2 double f(double x,double H,double h,double D)
 3 {
 4     double tanA,Y;
 5     tanA=(H-h)/x;//这里大家可以画个图帮助理解
 6     Y=H/tanA;//下面那条的长(在没有墙的情况下)
 7     if(Y<=D) return Y-x;//如果没到墙,就直接返回地上影子的长度。
 8     Y=Y-D;
 9     return D-x+Y*tanA;//D-X是地上影子的长度,Y*tanA是墙上影子的长度.
10 }
11 int main()
12 {
13     freopen("p3382.in","r",stdin);
14     int i,T;
15     double H,h,D;
16     double L,R,m1,m2,fm1,fm2;
17     scanf("%d",&T);
18     for(i=0;i<T;i++)
19     {
20         scanf("%lf%lf%lf",&H,&h,&D);
21         L=0;R=D;
22         while(L+1e-11<R)
23         {
24             m1=L+(R-L)/3; m2=R-(R-L)/3;
25             fm1=f(m1,H,h,D);  fm2=f(m2,H,h,D);
26             if(fm1<fm2) L=m1;
27             else R=m2;
28         }
29         printf("%.3lf\n",f(L,H,h,D));
30     }
31     return 0;
32 }

为何是一个单峰,可以考虑看一下第一个参考的博客。

理解时参考下图:

原文地址:https://www.cnblogs.com/huashanqingzhu/p/10961295.html

时间: 2024-08-01 02:49:46

[清华集训2015]灯泡(浙江大学ZOJ 3203 Light Bulb)的相关文章

ZOJ 3203: Light Bulb

ZOJ 3203: Light Bulb 1 ///@author Sycamore, ZJNU 2 ///@date 2017-02-09 3 #include<algorithm> 4 #include<iostream> 5 #include<iomanip> 6 #define eps 1e-10 7 using namespace std; 8 double H, h, L, D; 9 double getL(double a) 10 { 11 return

zoj 3203 Light Bulb,三分基础题

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 incommodio

ZOJ 3203 Light Bulb 三分

最简单的三分题,期末考完先做一道练练手 就是这么一个图,告诉你H h D,问你L最长是多少,假设人到灯的距离是X,那么容易得到 L = H-D/x*(H-h)+D-x,求个导很容易发现是一个关于x 的凸性函数,就可以三分啦 要注意的是三分的时候的精度eps,这题要求得是1e-9才能A,1e-8都WA,真是囧 #include <cstdio> #include <sstream> #include <fstream> #include <cstring> #

ZOJ 3203 Light Bulb (三分+计算几何)

题目地址:ZOJ 3203 第一发三分.三分的原理还是挺简单的. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include <set> #include <stdio.h&g

zoj 3203 Light Bulb(公式推导|三分法)(简单)

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 incommodio

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

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 incommodio

ZOJ Problem Set - 3203 Light Bulb 【三分法】

题目:ZOJ Problem Set - 3203 Light Bulb 题意: 如图,有个人在地上走,然后他的影子可以投影到墙上或者地上,问影子最长是多少? 分析: 我们知道,三分法是解决一个凹或凸函数的极大极小值,发现这个影子从刚好投影到右下角开始便是一个凸函数,他的影子长度是先递增后递减的,所以可以用三分法. 三分法的原理: AC代码: #include <cstdio> #include <cstring> #include <vector> #include

清华集训2015 V

#164. [清华集训2015]V http://uoj.ac/problem/164 统计 描述 提交 自定义测试 Picks博士观察完金星凌日后,设计了一个复杂的电阻器.为了简化题目,题目中的常数与现实世界有所不同. 这个电阻器内有编号为 1∼n1∼n 的 nn 个独立水箱,水箱呈圆柱形,底面积为 1 m21 m2,每个水箱在顶部和底部各有一个阀门,可以让水以 1 m3/s 的流量通过,每个水箱的上阀门接水龙头,可以无限供应水,下阀门不接东西,可以让水流出.水箱顶部和底部都有一个接口,水的电

uoj164. 【清华集训2015】V 统计

坑爹题面:http://uoj.ac/problem/164 正常题面: 对于一个序列支持下列5个操作: 1.区间加x 2.区间减x并与0取max 3.区间覆盖 4.单点查询 5.单点历史最大值查询 题解: 每个区间维护一个标记函数f(x)=max(x+a,b) 那么两个标记 f 和 g 的合并就是f(g(x))=max(x+max(fa+ga,-inf),max(fb+ga,gb))(假设f在前g在后) 区间加减就是打上max(x,0),区间覆盖就是打上max(-inf,x) 只要记录历史最大