题目:【HDU】 3714 Error Curves
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3714
题目描述:给出了多个函数,每个对应自变量x对应各函数中的最大值构成新函数,找其最小值。
题目分析:这个新函数必定是向下凸函数(不会证),正如二分法针对单调函数找零点,这里有三分法来找最值,前提函数凹凸性不变,即f’’(x)保号,且存在f’(x)=0。
代码:
#include <stdio.h>
int a[10020],b[10020],c[10020];
int n;
double f(double x) //新函数
{
double max,f;
int i;
max=a[0]*x*x+b[0]*x+c[0];
for(i=1;i<n;i++)
{
f=a[i]*x*x+b[i]*x+c[i];
if(f>max) max=f;
}
return max;
}
int main()
{
int i,t;
double l,r,p,q;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d%d%d",a+i,b+i,c+i);
}
l=0,r=1000;
while(r-l>1e-10)
{ p=l-(l-r)/3,q=r-(r-l)/3; //这里的三分是三等分
if(f(p)<f(q)) r=q; //其实可以双重二等分
else l=p; //强迫症喜欢前者
}
printf("%.4lf\n",f(p));
}
return 0;
}