Link: http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemcode = 3203 http://acm.hust.edu.cn/vjudge/contest/view.action? Cid = 28708 # Problem/ 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 integerT(T<= 100), indicating the number of instances.
Each test case contains three real numbersH,HAndDIn one line.HIs the height of the light bulb whileHIs the height of mildleopard.DIs
Distance between the light bulb and the wall. All numbers are in range from 10-2 to 103, both wide, andH-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
32 1 0.52 0.5 34 3 4
Sample output
1.0000.7504.000
Author: Guan, Yao
Source: The 6th Zhejiang Provincial Collegiate Programming Contest
Submit status
Algorithm: using the Convex Function Method 1: When l = 0, the smaller the inclination angle, the longer the shadow, so when l is exactly 0, the length of X is the left limit.
(H-h)/x = h/d
So X = (h-h) * D/h, Left = x = (h-h) * D/h
Obviously, the limit on the right is X = D, so right = D.
That is, if the value range of X is [left, right]
The following analysis assumes that X has known, how to find Shadow = D-X + L
L/h =? /(D + ?) ........................................ .. (1)
Tan (A) = L /? = (H-h)/X can get? = L * x/(h-h) into (1)
We can obtain L = H-D * (h-h)/x
So Shadow = D-x + L = d + H-[x + (h-h) * D/X]
To maximize shadow, the function value in [] is the smallest.
Note that the value in [] is determined by X, and we have already determined the range of X,
It is easy to see that the function in [] is a single-peak function:
The condition that is set based on a ^ 2 + B ^ 2> = 2AB Is A = B, so we can determine that if x = (h-h) * D/X is the value of x0
The result is obvious to minimize the value in. Since the extreme point can be determined, there is no need to use three points.
In the following three cases, the X that meets the conditions can be obtained to maximize the shadow:
(1) The value range of X covers the monotonic decreasing range of the value range of x0: x = x0 (2) x [Right <= x0]: x = right (3) the range of the value range of X increases monotonically [left> = x0]: x = left
The following is the answer to bring the X to the Shadow formula above.
I want to calculate the extreme values of a function mainly because the X in the Three-Point Graph is complicated, although the three points in this step are the same as the extreme values. However, if we really use the three-point idea, we still think that the three-point l of Jing hongshile kids shoes is better. Although we cannot easily find the extreme point, but at least we can see at a glance that the value range of L is [0, H]. If all the three points can find the extreme point, the meaning of the three points is lost.
School girl's idea: 3-point lhttp: // www.cnblogs.com/riddle/p/3251109.html
Make Shadow = S + LPS: S is D-X.
By L/h =? /(D + ?) Can be obtained? = (L * D)/(H-L)
L/h =? /(S + ?) S =? * (H-l)/L = D * (H-l)/(H-L) before the L range has been determined [0, H]
Shadow = S + L. Children have better ideas.
Code: Train of Thought 1:
#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 0;}