三分(TOJ3777)
2018-03-20 本文已影响0人
我好菜啊_
- 求解凸性函数的极值
先输入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)的最小值
输入样例:
2
2 0 0
2 0 0
2 0 0
2 -4 2
输出样例:
0.0000
0.5000
三分.jpg
#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;
double a1, b1, c1, a2, b2, c2;
double f(double e)
{
//计算F(x)
return max(a1*e*e + b1*e + c1, a2*e*e + b2*e + c2);
//max的头文件是algorithm
}
int main()
{
int t;
cin >> t;
while (--t) {
cin >> a1 >> b1 >> c1 >> a2 >> b2 >> c2;
double left = 0, right = 1000;//初始化上下界
while (right - left >= 1e-8) {
//注意此处while的条件,因为不止是整数
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 << setiosflags(ios::fixed) << setprecision(4) << f(left) << endl;
}
system("pause");
return 0;
}