第三个数据点是否有误?
查看原帖
第三个数据点是否有误?
478461
_lfxxx_楼主2022/10/22 18:53

我写了个爬山结果 WA 了 #3,而且答案差得很多,下了数据点我按步长 10310^{-3} 输出函数值,结果发现函数先减后增再减,下面是数据和代码: in:

11 -0.998244353 1.4899310583818
-72.904599140644 67.001717998916 93.773554224846 -55.793191403459 -38.366589898599 9.444119273570 -62.323604790564 98.576260383561 99.292265109602 93.538987402101 45.167792642378 96.221938355388

ans:

1.2851759191974

code:

#include<bits/stdc++.h>
using namespace std;
#define db double
int n;
db ans,d,a[15],l,r;
inline db calc(db x){
    db res=0;
    for(int i=0;i<=n;++i)
        res=res*x+a[i];
    return res;
}
void HillClimb(){
	for(db t=10;t>1e-6;t*=0.999){
        db y;
        if(ans+t<=r){
            y=calc(ans+t);
            if(y>d) ans+=t,d=y;
        }
        if(ans-t>=l){
            y=calc(ans-t);
            if(y>d) ans-=t,d=y;
        }
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
	cin>>n>>l>>r;
	for(int i=0;i<=n;++i)
        cin>>a[i];
    ans=(l+r)/2,d=calc(ans);
//    for(db i=l;i<=r;i+=1e-3)cerr<<calc(i)<<'\n'; 调试输出,提交时没有这行
    HillClimb();
    cout<<ans<<endl;
    return 0;
}
2022/10/22 18:53
加载中...