三分模板。
三分法求单峰函数最优值,之后每次取所有二次函数最优值即可
1 #pragma GCC optimize(3,"Ofast","inline") 2 #include<iostream> 3 #include<cstdio> 4 #define N 100005 5 #define eps 1e-9 6 using namespace std; 7 int read() 8 { 9 int x=0,f=1;char ch=getchar(); 10 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 11 while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();} 12 return x*f; 13 } 14 int t,n; 15 double ans,a[N],b[N],c[N]; 16 double check(double x) 17 { 18 double r=-21374404; 19 for(int i=1;i<=n;i++) 20 { 21 r=max(r,double(a[i]*x*x+b[i]*x+c[i])); 22 } 23 return r; 24 } 25 signed main() 26 { 27 //freopen("1.in","r",stdin); 28 t=read(); 29 while(t--) 30 { 31 n=read(); 32 for(int i=1;i<=n;i++) 33 { 34 a[i]=read(); 35 b[i]=read(); 36 c[i]=read(); 37 } 38 double l=0,r=1000,lmid,rmid; 39 while(l+eps<r) 40 { 41 rmid=r-(r-l)/3.0; 42 lmid=l+(r-l)/3.0; 43 if(check(lmid)<=check(rmid))r=rmid; 44 else l=lmid; 45 } 46 printf("%.4lf ",check(l)); 47 } 48 return 0; 49 }