zoukankan      html  css  js  c++  java
  • HDU2899 Strange fuction(模拟退火)

    模拟退火模板,按比例接受

    #include<bits/stdc++.h>
    using namespace std;
    const int N=1e5+10;
    const double eps=1e-8;
    double ans;
    double y;
    double cal(double x){return 6*pow(x,7.0)+8*pow(x,6.0)+7*pow(x,3.0)+5*pow(x,2.0)-y*x;}
    int f[2]={1,-1};
    double solve(double tmp){
        double res=1e18;
        double cur;
        double sx=50.0;
        cur=cal(sx);
        double dc=0.98;
        res=min(res,cur);
        while(tmp>eps){
            double nx=sx+f[rand()%2]*tmp;
            if(nx>100.0||nx<0.0);
            else{
                double nxd=cal(nx);
                res=min(res,nxd);
                if(cur-nxd>eps)sx=nx,cur=nxd;
                else if(exp(-(nxd-cur)/tmp)*RAND_MAX>rand()){
                    sx=nx,cur=nxd;
                }
            }
            tmp*=dc;
        }
        return res;
    }
    int main(){
        //ios::sync_with_stdio(false);
        int t;
        cin>>t;
        while(t--){
            cin>>y;
            ans=solve(100);
            printf("%.4f
    ",ans);
        }
    }
    View Code
    没有人不辛苦,只有人不喊疼
  • 相关阅读:
    P1182 数列分段`Section II` 二分
    洛谷 P1025 数的划分
    深浅拷贝
    数据的内置方法
    控制流程-if/while/for
    python的基本运算符
    花式赋值
    python的注释
    Python的垃圾回收机制
    变量与常量
  • 原文地址:https://www.cnblogs.com/ctyakwf/p/13849840.html
Copyright © 2011-2022 走看看