题目:http://acm.hdu.edu.cn/showproblem.php?pid=2899
还可三分。不过只写了模拟退火。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<ctime> #include<cmath> #include<cstdlib> #define db double using namespace std; const int nx[6]={0,7,6,3,2,1},c[6]={0,6,8,7,5,1}; const db dc=0.997,eps=1e-17; int T; db y,ans,px; db pw(db x,int k){db ret=1;while(k){if(k&1)ret*=x;x*=x;k>>=1;}return ret;} db calc(db x) { db ret=0; for(int i=1;i<=4;i++)ret+=c[i]*pw(x,nx[i]); ret-=y*x; return ret; } db gtrd(db T){return (rand()*2-RAND_MAX)*T;} void SA(db T) { db x0=px,pr=ans,x,cr; while(T>eps) { x=x0+gtrd(T); if(x>100)x=100; if(x<0)x=0; cr=calc(x); if(cr<pr||exp((cr-pr)/T)*RAND_MAX<rand()) { if(cr<ans)ans=cr,px=x; x0=x;pr=cr; } T*=dc; } } int main() { srand(time(0)); scanf("%d",&T); while(T--) { scanf("%lf",&y); px=50;ans=calc(px); for(int i=1;i<=2;i++)SA(10000); printf("%.4lf\n",ans); } return 0; }
原文地址:https://www.cnblogs.com/Narh/p/9886307.html
时间: 2024-11-13 13:30:07