Error Curves

10 篇文章 0 订阅

http://acm.hdu.edu.cn/showproblem.php?pid=3714

本题用三分搜索法,应该也算简单的三分应用了,正好借这题学了一下三分

这里总结一下三分

先找到左边界left和右边界right

然后取mid=(left+right)/2.0,midmid=(mid+right)/2.0;

然后比较mid,midmid然后根据你的图像赋值left或者right

代码:


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
struct node
{
    double a,b,c;
}f[10005];
int n;
double max(double a,double b)
{
    if(a>b)
    return a;
    else
    return b;
}
double cal(double x,double a,double b,double c)
{
    return a*x*x+b*x+c;
}
double maxmap(double x)
{
    double maxn=-1e10;
    for(int i=0;i<n;i++)
    maxn=max(maxn,cal(x,f[i].a,f[i].b,f[i].c));
    return maxn;
}
double three()
{
    double left=0;
    double right=1000.0;
    for(int i=0;i<=100;i++)
    {
        double mid=(left+right)/2.0;
        double midmid=(mid+right)/2.0;
        double mleft=maxmap(mid);
        double mright=maxmap(midmid);
        if(mleft<mright)
        right=midmid;
        else
        left=mid;
    }
    double ans=maxmap(left);
    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",&f[i].a,&f[i].b,&f[i].c);
        printf("%.4f\n",three());
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值