退火求助
查看原帖
退火求助
161748
ssilrrr楼主2022/8/28 18:53

rt,97分,貌似被卡精度了

#include <bits/stdc++.h>
using namespace std;

template <class T>
class Safe_TuiHuo{
private:
    double rand_in(double L,double R){
        double seed=1.0*rand()*rand()*rand()/rand()/rand();
        return L+fmod(seed,(R-L));
    }
    double rand_bet(double L,double R){
        int seed=rand()%2;
        return (seed==0)?L:R;
    }
    void makesafe(double L,double R,double &th){
        if(th<L)th=L;
        else if(th>R)th=R;
    }
public:
    int calands=0;
    T (*f)(double);
    bool (*cmp)(T,T);
    void init(T (*_f)(double)){
        f=_f;
    }
    void initcmp(bool (*_cmp)(T,T)){
        cmp=_cmp;
    }
    void initrnd(){
        srand((unsigned)time(0));
    }
    double run(double maxinj,double mininj,double cxdown,double L,double R){
        calands=0;
        auto begin=rand_in(L,R);
        auto maxn=maxinj;
        while(maxinj>mininj){
            calands++;
            double range=(1.0*(R-L)/2)*(1.0*maxinj/maxn);
            auto nex=begin+rand_bet(-range,range);
            makesafe(L,R,nex);
            if(cmp(f(nex),f(begin))){
                begin=nex;
            }
            maxinj=maxinj*cxdown;
        }
        return begin;
    }
    int effi(){
        return calands;
    }
};


int T;
double H,h,D;
double f(double x){
    return D+H-(x+(D*(H-h)/x));
}
bool cmp(double x,double y){
    return x>y;
}
Safe_TuiHuo<double> sth;
int main(){
    sth.init(f);
    sth.initcmp(cmp);
    cin>>T;
    while(T--){
        cin>>H>>h>>D;
        double k=sth.run(1e9+7,1e4,0.99,D*(H-h)/H,D);
        printf("%.3lf\n",f(k));
    }
}

2022/8/28 18:53
加载中...