链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4222
题意:定义F(x) = max(Si(x)), i = 1...n. x 的范围为[0, 1000]. Si(x)=ai x2 + bi x + ci .
思路:F(x)是一个单峰函数,用三分法求极值,注意下精度。
#include<cstdio> #include<algorithm> using namespace std; const int maxn=10000+5; int a[maxn],b[maxn],c[maxn],n; const double eps=1e-9; double F(double x) { double maxm=a[0]*x*x+b[0]*x+c[0]; for(int i=1;i<n;i++) maxm=max(maxm,a[i]*x*x+b[i]*x+c[i]); return maxm; } int main() { int t; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i=0;i<n;i++) scanf("%d%d%d",&a[i],&b[i],&c[i]); double x=0.0,y=1000.0; while(x+eps<y) //for(int i=0;i<100;i++) { double m1=x+(y-x)/3; double m2=y-(y-x)/3; if(F(m1)<F(m2)) y=m2; else x=m1; } printf("%.4lf\n",F(y)); } return 0; }
究竟是我抛弃了历史,还是历史遗弃了我。