题目: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=;
for(;b;b>>=,a=a*a)
if(b&)ret=ret*a;
return ret;
}
db cal(db x){return *pw(x,)+*pw(x,)+*pw(x,)+*pw(x,)-y*x;}
void SA()
{
db T=,x=ansx,lst=ans,tx,tmp;
while(T>eps)
{
tx=x+(rand()*-RAND_MAX)*T;
if(tx>)tx=; if(tx<)tx=;
tmp=cal(tx); db delt=tmp-lst;
if(delt<||exp(delt/T)*RAND_MAX<rand())x=tx,lst=tmp;
if(tmp<ans)ans=tmp;
T*=dt;
}
}
int main()
{
srand(time());
int T; scanf("%d",&T);
while(T--)
{
scanf("%lf",&y);
ans=cal(); ansx=;
SA();
printf("%.4lf\n",ans);
}
return ;
}