题目地址:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4222
思路:
几个函数图像相交所形成的图像仍为下凸的(若不为下凸,则在交点处存在先增后减,而由于取局部最大值,不应该选择递减部分,矛盾,所以图像全部为下凸),下凸函数最小值可用三分法解决:取区间[l,r]的两个三分点m1和m2,比较F(m1)和F(m2)大小。若F(m1)<F(m2),则解在[l,m2]中;否则解在[m1,r]中。
#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define debug
using namespace std;
const double eps=1e-6;
const int maxn=1e4+50;
int n;
double a[maxn],b[maxn],c[maxn];
double val(double x)
{
double tmp=a[0]*x*x+b[0]*x+c[0];
for(int i=1;i<n;i++)
{
tmp=max(tmp,a[i]*x*x+b[i]*x+c[i]);
}
return tmp;
}
int main()
{
#ifdef debu
freopen("in.txt","r",stdin);
#endif // debug
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=0; i<n; i++)
{
scanf("%lf%lf%lf",&a[i],&b[i],&c[i]);
}
double l=0.0,r=1000.0,ans;
for(int step=0;step<1000;step++)
{
double mid1=l+(r-l)/3;
double mid2=r-(r-l)/3;
if(val(mid1)<val(mid2))
{
r=mid2;
}
else
{
l=mid1;
ans=l;
}
}
printf("%.4f\n",val(ans));
}
return 0;
}