A. Light Bulb

Time Limit: 1000ms
Case Time Limit: 1000ms
Memory Limit: 32768KB
 
64-bit integer IO format: %lld      Java class name: Main
 

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 解题:好吧,比较喜欢数学解法,速度快嘛。。。参阅某神的代码。。。

算法:利用函数的凸性

 
 

思路一:

 
 
 L = 0 时,倾斜角越小 shadow 越长
所以 L 正好为 0 时,  X 的长度为左极限 
 
(H-h)/ X = H / D 
 
所以 X =  (H-h)*D / H ,left = X = (H-h)*D / H
 
明显可以看到右边的极限就是 X = D , 从而 right = D
 
也就是如果用所谓的三分法 X 的取值区间是 【left, right】
 
下面再分析假设 X 已经知道了,如何求出 shadow = D-X+L
 
 
L / H = ? / (D + ?) ..........................................(1)
 
tan(a) = L / ? = (H-h) / X  可以得到 ? = L *X / (H-h) 带入 (1)
 
可以得到 L = H - D*(H-h)/ X
 
所以 shadow = D - X + L = D+H - 【X+ (H-h)*D / X】
 
要使得 shadow 最大 , 则 【】中的函数值最小
 
注意到 【】中的值由  X 确定 ,而 前面我们已经确定了 X 的区间,
 
容易看出 【】中的函数是个单峰函数:
 
 
根据 a^2 + b^2 >= 2ab 成立的条件是 a = b
所以我们可以确定如果 X = (H-h)*D / X 求出的值就是上图中的 X0
 
那么要使得【】中的值最小,结果是显然的了。既然能确定极值点,那么也没有必要用三分了
 
分下面三种情况即可求出满足条件的 X 使得shadow 最大 :
 
(1) X 的取值范围覆盖了 X0: X = X0
(2) X 的取值范围所在的区间单调递减【right <= X0】: X = right
(3) X 的取值范围所在的区间单调递增【left >= X0】: X = left
 
把所求的 X 带入上面的shadow 公式就是答案了。
 
想到根据函数的极值求主要是想的是三分图中的 X 本来就比较复杂了,虽然弄到这一步三分和求极值都一样。
但是如果真的用三分的思路的话, 还是觉得荆红浅醉 童鞋的三分 L 比较好,
虽然无法轻易的求出极值点,但是至少可以一眼看出 L 的取值范围是【0, H】话说要是所有的三分都能求出极值点,那么也 就失去了三分的意义了。
 

学妹的思路:三分 L

 
令所求 shadow =  S + L
PS : S 就是上图的 D - X
 
由 L / H = ? / (D+?)      可以得到  ?= (L*D)  /  (H-L)
 
而 L / h = ? / (S+ ?)      可得 S = ?* (h-L) / L   =  D*(h-L) / (H-L)   前面又已经确定了 L 的范围【0, H】
 
shadow = S + L 随便乱搞都可以三分出来了。

 #include<stdio.h>
#include<string.h>
#include<math.h> int main()
{
int T;
double H,h,D;
scanf("%d", &T);
while(T--)
{
scanf("%lf%lf%lf", &H,&h,&D);
double x1 = (H-h)*D/H;
double x2 = D;
double x0 = sqrt(D*(H-h)); double x; if(x1 <= x0 && x0 <= x2) x = x0;
else if(x0 <= x1) x = x1;
else if(x0 >= x2) x = x2; double ans = D+H- (x + (H-h)*D/x);
printf("%.3lf\n", ans);
}
return ;
}

A. Light Bulb的更多相关文章

  1. 三分 --- ZOJ 3203 Light Bulb

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

  2. Light Bulb(三分)

    ZOJ Problem Set - 3203 Light Bulb Time Limit: 1 Second      Memory Limit: 32768 KB Compared to wildl ...

  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 (三分+计算几何)

    B - Light Bulb Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit ...

  6. ZOJ 3203 Light Bulb (三分查找)

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

  7. ZOJ 3203 Light Bulb

    Compared to wildleopard's wealthiness, his brother mildleopard is rather poor. His house is narrow a ...

  8. ZOJ 3203 Light Bulb(数学对勾函数)

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

  9. TOJ 2814 Light Bulb

    Description Compared to wildleopard's wealthiness, his brother mildleopard is rather poor. His house ...

随机推荐

  1. javaScript中的闭包原理 (译)

    这篇文章通过javaScript代码解释了闭包的原理,来让编程人员理解闭包.它不是写给大牛或使用功能性语言进行编程的程序员的.一旦意会了其核心概念,闭包理解起来并不难.然而,你不可能通过阅读任何有关闭 ...

  2. PL/SQL 多表关联UPDATE

    假设有两个表A和B,A表字段a,b,c,d,B表字段b,e,f,两表的关联条件是字段b,现在想做个data patch,欲将B表中的字段e的值patch给A表的字段c. 有如下两种方法: 1 upda ...

  3. 5个典型的JavaScript面试题

    在IT界,需要大量的 JavaScript 开发者.如果你的能力能够胜任这一角色,那么你将获得许多换工作和提高薪水的机会.但是在你被公司录取之前,你需要展现你的技术,以便通过面试环节.在这篇文章中,我 ...

  4. Sql 行转换为列 以及列转换为行的心得

    这是 创建数据库的脚本文件 CREATE TABLE [dbo].[stu]( [学号] [nvarchar](255) NOT NULL, [姓名] [nvarchar](255) NULL, [性 ...

  5. JavaEE汇总

    1.简述Spring. a)      Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,其目的是解决企业应用开发的复杂性,能够使用基本的JavaBean代替EJB,并提供了 ...

  6. iphone之打开pdf、doc、xls文件用UIWebView

    //文件名字及类型 NSString *path=[[NSBundle mainBundle]pathForResource:@"xls1" ofType:@"xls&q ...

  7. tomcat 发布本地文件

    应用场景,通过web,jsp访问本地mouse文件夹的静态文件 通过修改tomcat配置文件server.xml <!--在Host标签下加入Context标签,path指的是服务器url请求地 ...

  8. lambda表达式的简单入门

    前言:本人在看<Java核心技术I>的时候对lamdba表达式还不是太上心,只是当做一个Java 8的特性了解一下而已,可是在<Java核心技术II>里面多次用到,所以重新入门 ...

  9. Python学习日志9月15日

    一周就要过去了,而我跟一周以前没什么区别.回想一下,我这周做了什么事情呢.恍然若失.这周的精力都浪费在很多不必要的事情上了.学过一片古文,讲后羿学射箭,他有一个同学跟他一样聪明,在一起学习.后羿呢,专 ...

  10. hibernate的注解

    1.many-to-one @ManyToOne @JoinColumn(name = "user_id") 2.many-to-many /** * 双向关联关系中,有且仅有一端 ...