LA 5009 (三分法求极值) Error Curves

给出的曲线要么是开口向上的抛物线要么是直线,但所定义的F(x)的图形一定是下凸的。

注意一点就是求得是极小值,而不是横坐标,样例也很容易误导人。

 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 
 5 const int maxn = 10000 + 10;
 6 
 7 int n;
 8 double a[maxn], b[maxn], c[maxn];
 9 
10 double F(double x)
11 {
12     double ans = a[0]*x*x+b[0]*x+c[0];
13     for(int i = 1; i < n; i++)
14         ans = max(ans, a[i]*x*x+b[i]*x+c[i]);
15     return ans;
16 }
17 
18 int main()
19 {
20     //freopen("in.txt", "r", stdin);
21 
22     int T;
23     scanf("%d", &T);
24     while(T--)
25     {
26         scanf("%d", &n);
27         for(int i = 0; i < n; i++) scanf("%lf%lf%lf", &a[i], &b[i], &c[i]);
28         double L = 0.0, R = 1000.0;
29         for(int i = 0; i < 80; i++)
30         {
31             double m1 = L + (R-L)/3;
32             double m2 = R - (R-L)/3;
33             if(F(m1) < F(m2)) R = m2;
34             else L = m1;
35         }
36         printf("%.4f\n", F(L));
37     }
38 
39     return 0;
40 }
代码君

 

posted @ 2015-03-14 23:23  AOQNRMGYXLMV  阅读(247)  评论(0编辑  收藏  举报