Error Curves UVA

来源:互联网 发布:如何修改网站数据库 编辑:程序博客网 时间:2024/05/17 17:54

题目链接:https://vjudge.net/problem/UVA-1476

题意:S(x) = a * x ^ 2 + b * x + c,给定n个a b c的值从而确定n个S(x)方程式,F(x) = max(Si(X)),求F(x)最小值。

思路:看到S(x)定义发现其是一个先减后增(凹函数)的函数(0 <= a <= 100),很明显可以用三分求最值,当a为0时为单调函数用三分也用适用。根据F(x)的定义不难发现F(x)也一定是一个单峰函数,可以用三分求最值。

代码如下:

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<cstdlib>#include<sstream>#include<deque>#include<stack>#include<set>#include<map>using namespace std;typedef long long ll;typedef unsigned long long ull;const double eps = 1e-9;const int  maxn = 10000 + 20;const int  maxt = 300 + 10;const int mod = 10;const int dx[] = {1, -1, 0, 0};const int dy[] = {0, 0, -1, 1};const int Dis[] = {-1, 1, -5, 5};const double inf = 0x3f3f3f3f;const int MOD = 1000;const double PI = acos(-1.0);int n, m, k;struct node{    double a, b, c;}num[maxn];double solve(double x){//F(x)    double ans;    ans = num[0].a * x * x + num[0].b * x + num[0].c;    for(int i = 1; i < n; ++i){        ans = max(ans, num[i].a * x * x + num[i].b * x + num[i].c);    }    return ans;}int main(){    int t;    scanf("%d", &t);    while(t--){        scanf("%d", &n);        for(int i = 0; i < n; ++i){            scanf("%lf%lf%lf", &num[i].a, &num[i].b, &num[i].c);        }        double ans = inf;        double l = 0.0, r = 1000.0;        double mid, mmid, tmp1, tmp2;        while(l + eps < r){//三分求凹函数最小值            mid = (l + r) / 2.0;            mmid = (mid + r) / 2.0;            tmp1 = solve(mid);            tmp2 = solve(mmid);            if(tmp1 > tmp2) l = mid;            else r = mmid;        }        printf("%.4f\n", solve(l));    }    return 0;}


原创粉丝点击