【模板】三分法

模拟退火(SA)学习link
玄学摸你退火,精度坑逼,玄学调参。。。
AC代码

#include<bits/stdc++.h>
#define re register int
using namespace std;
int n;
double l,r,ans=-99999999,coe[20];
double T=0.95,t=0.99,mt=1e-14,wzzorz;
double get_ans(double x)
{
    double s=0,jc=1;
    for(re i=n+1;i>=1;i--)
    {
        s=s+coe[i]*jc;
        jc=jc*x;
    }
    return s;
}
void SA()
{
    double now=(l+r)/2;
    double tt=T;
    while(tt>mt)
    {
        double temp=now+(rand()*2-RAND_MAX)*tt;
        if(temp<l) temp=l;
        if(temp>r) temp=r;
        double now_ans=get_ans(temp);
        if(now_ans-ans>0)
        {
            ans=now_ans;
            now=temp;
            wzzorz=temp;
        }
        tt*=t;
    }
}
void work()
{
    for(re i=1;i<=8;i++)
    {
        SA();
    }
    return;
}
int main()
{
    srand(time(0));
    cin>>n>>l>>r;
    for(re i=1;i<=n+1;i++)
    {
        cin>>coe[i];
    }
    work();
    printf("%.5lf",wzzorz);
    return 0;
}
posted @ 2019-06-15 11:07  Leinsea  阅读(97)  评论(0编辑  收藏  举报
Live2D //博客园自带,可加可不加