Light Bulb 三分入门题

                      Light Bulb

分析:求出公式,发现在给定范围内是凸函数。三分求最值。

 1 /********************************
 2 please don't hack me!! /(ToT)/~~
 3                 __------__
 4               /~          ~\
 5              |    //^\\//^\|
 6            /~~\  ||  T| |T|:~\
 7           | |6   ||___|_|_||:|
 8            \__.  /      o  \/'
 9             |   (       O   )
10    /~~~~\    `\  \         /
11   | |~~\ |     )  ~------~`\
12  /' |  | |   /     ____ /~~~)\
13 (_/'   | | |     /'    |    ( |
14        | | |     \    /   __)/ \
15        \  \ \      \/    /' \   `\
16          \  \|\        /   | |\___|
17            \ |  \____/     | |
18            /^~>  \        _/ <
19           |  |         \       \
20           |  | \        \        \
21           -^-\  \       |        )
22                `\_______/^\______/
23 ************************************/
24 
25 #include <iostream>
26 #include <cstdio>
27 #include <cstring>
28 #include <cmath>
29 #include <algorithm>
30 #include <string>
31 #include <vector>
32 #include <set>
33 #include <map>
34 #include <queue>
35 #include <stack>
36 #include <cstdlib>
37 #include <sstream>
38 using namespace std;
39 typedef long long LL;
40 const LL INF = 0x5fffffff;
41 const double EXP = 1E-8;
42 const LL MOD = (LL)1E9+7;
43 const int MS = 1005;
44 
45 double H, h, D;
46 
47 double fun(double x) {
48     return (h - H) * D / (D - x) + H + x;
49 }
50 
51 int main() {
52     int T;
53     scanf("%d",&T);
54     while (T--) {
55         scanf("%lf%lf%lf",&H,&h,&D);
56         double l = 0;
57         double r = h * D / H;
58         while ((r - l) >= EXP) {
59             double mid = (l + r) / 2;
60             double mmid = (mid + r) / 2;
61             if (fun(mid) > fun(mmid))
62                 r = mmid;
63             else
64                 l = mid;
65         }
66         printf("%.3lf\n",fun(l));
67     }
68     return 0;
69 }

 

posted on 2015-08-16 21:13  hutaishi  阅读(210)  评论(0编辑  收藏  举报

导航