题意:
先输入n表示共n组数据。每组数据为两个二次函数Y1,Y2的系数(Y=Ax^2+Bx+C,0<=A<=100,0<=|B|<=5000,|C|<=5000),令F(x)=max(Y1(x), Y2(x)),在定义域[0,1000]内求F(x)的最小值。
思路:
Y时一个开口向上的二次函数,F(x)=max(Y1(x), Y2(x)),F函数要么是一个先递减后递增的下凸函数,要么是一个单调函数,
使用三分法,即求的区间内的最小值。
代码:
#include<iostream> #include<stdio.h> #include<stdlib.h> #include<string> #include<iomanip> #include<algorithm> #include<string.h> #include<queue> #include<cmath> using namespace std; const int maxn=1e5+10; const int inf=1e10; typedef long long ll; double a1,b1,c1,a2,b2,c2; double f(double x){ return max(a1*x*x+b1*x+c1,a2*x*x+b2*x+c2); } int main() { int t; cin>>t; while(t--) { cin>>a1>>b1>>c1; cin>>a2>>b2>>c2; double left=0,right=1000; while(right-left>1e-8) { double mid1=left+(right-left)/3; double mid2=right-(right-left)/3; if(f(mid1)<f(mid2)+1e-8) right=mid2; else left=mid1; } cout<<fixed<<setprecision(4)<<f(left)<<endl; } system("pause"); return 0; }