三分 HDOJ 3714 Error Curves
1 /*
2 三分:凹(凸)函数求极值
3 */
4 #include <cstdio>
5 #include <algorithm>
6 #include <cstring>
7 #include <cmath>
8 using namespace std;
9
10 const int MAXN = 1e4 + 10;
11 const int INF = 0x3f3f3f3f;
12 const double EPS = 0.0000000001;
13 struct F {
14 double a, b, c;
15 }f[MAXN];
16 int n;
17
18 double cal(double x) {
19 double res = -INF;
20 for (int i=1; i<=n; ++i) {
21 res = max (res, f[i].a * x * x + f[i].b * x + f[i].c);
22 }
23 return res;
24 }
25
26 int main(void) { //HDOJ 3714 Error Curves
27 //freopen ("HDOJ_3714.in", "r", stdin);
28
29 int T; scanf ("%d", &T);
30 while (T--) {
31 scanf ("%d", &n);
32 for (int i=1; i<=n; ++i) {
33 scanf ("%lf%lf%lf", &f[i].a, &f[i].b, &f[i].c);
34 }
35
36 double l = 0.0, r = 1000.0;
37 for (int i=1; i<=100; ++i) {
38 double mid = (l + r) / 2;
39 double rmid = (mid + r) / 2;
40 if (cal (mid) < cal (rmid)) r = rmid;
41 else l = mid;
42 }
43 printf ("%.4f\n", cal (r));
44 }
45
46 return 0;
47 }
编译人生,运行世界!