[日常摸鱼]三分法

翻到一个三分法的模板发现没有写掉…今天干脆写掉算了…(luogu3382)

跟二分基本差不多…

#include<cstdio>
typedef double dl;
const double eps=1e-7;
const int N=20;
dl l,r,a[N];
int n;
inline dl calc(dl x)
{
    dl res=0,tx=1;
    for(register int i=0;i<=n;i++)
    {
        res+=tx*a[i];tx*=x;
    }
    return res;
}
int main()
{
    scanf("%d%lf%lf",&n,&l,&r);
    for(register int i=n;i>=0;i--)scanf("%lf",&a[i]);
    while(r-l>eps)
    {
        dl x1=l+(r-l)/3.0,x2=l+2.0*(r-l)/3.0;
        dl t1=calc(x1),t2=calc(x2);
        if(t1>t2)r=x2;
        else l=x1;
    }
    printf("%.5lf",l);
    return 0;
}

 

posted @ 2018-01-24 14:06  yoshinow2001  阅读(150)  评论(0)    收藏  举报