HDOJ Error Curves 计算几何

来源:互联网 发布:和越南人聊天软件 编辑:程序博客网 时间:2024/05/17 01:24

题意:在给定的n个抛物线中,求 min(F(x)), F(x) = max(S(x)).

思路:由于给定的抛物线都是开口向上的(0 ≤ a ≤ 100)F(x)也是开口向上的类抛物线,具有单峰值,利用三分求解 。

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;#define eps 1e-10const int MAX = 10009;struct para {    int a, b, c;};para p[MAX];int n;double getmax(double x) {    double ans, tmp;    ans = p[0].a*x*x + p[0].b*x + p[0].c;    int i;    for (i = 1; i < n; ++i) {        tmp = p[i].a*x*x + p[i].b*x + p[i].c;        if (ans < tmp)            ans = tmp;    }    return ans;}int main(){    int t;    double l, r, mid1, mid2, max1, max2;    scanf("%d",&t);    while (t--) {        scanf("%d", &n);        int i;        for (i = 0; i < n; ++i)            scanf("%d %d %d", &p[i].a, &p[i].b, &p[i].c);        l = 0.0;        r = 1000.0;        while (fabs(r-l) > eps) {            mid1 = (l+r)/2;            mid2 = (l+mid1)/2;            max1 = getmax(mid1);            max2 = getmax(mid2);            if (max1 > max2)                r = mid1;            else                l = mid2;        }        printf("%.4lf\n", max1);    }    return 0;}