hdu 2899 Strange fuction —— 模拟退火
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2899
模拟退火;
怎么也过不了,竟然是忘了写 lst = tmp ...
还是挺容易A的。
代码如下:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<cstdlib> #include<ctime> #define eps 1e-17 #define dt 0.98 using namespace std; typedef double db; db y,ans,ansx; db pw(db a,int b) { db ret=1; for(;b;b>>=1,a=a*a) if(b&1)ret=ret*a; return ret; } db cal(db x){return 6*pw(x,7)+8*pw(x,6)+7*pw(x,3)+5*pw(x,2)-y*x;} void SA() { db T=1000,x=ansx,lst=ans,tx,tmp; while(T>eps) { tx=x+(rand()*2-RAND_MAX)*T; if(tx>100)tx=100; if(tx<0)tx=0; tmp=cal(tx); db delt=tmp-lst; if(delt<0||exp(delt/T)*RAND_MAX<rand())x=tx,lst=tmp; if(tmp<ans)ans=tmp; T*=dt; } } int main() { srand(time(0)); int T; scanf("%d",&T); while(T--) { scanf("%lf",&y); ans=cal(50); ansx=50; SA(); printf("%.4lf\n",ans); } return 0; }