题意:
n个二次函数,求定义域[0, 1000]时候每个函数的最小值当中的最大值。精确到1e-4
解决:
三分,eps = 1e-9能过,因为二次函数的函数值精确到1e-4所以自变量x精确度必须高于1e-8
1 #include <bits/stdc++.h> 2 3 const int MAXN = 1e4+10; 4 double eps = 1e-9; 5 6 int n; 7 int a[MAXN], b[MAXN], c[MAXN]; 8 9 double fun(double x) 10 { 11 double res = a[1] * x*x + b[1]*x + c[1]; 12 for (int i = 2; i <= n; ++i) { 13 res = std::max(res, a[i] * x *x + b[i] * x + c[i]); 14 } 15 return res; 16 } 17 18 int main() 19 { 20 int T; 21 scanf("%d", &T); 22 while (T--) { 23 scanf("%d", &n); 24 for (int i = 1; i <= n; ++i) 25 scanf("%d%d%d", a+i, b+i, c+i); 26 double l = 0, r = 1000; 27 while ( (r - l) > eps) { 28 // printf("l = %f, r = %f\n", l, r); 29 double ml = l + (r - l) / 3; 30 double mr = r - (r - l) / 3; 31 if (fun(ml) > fun(mr)) 32 l = ml + eps; 33 else 34 r = mr - eps; 35 } 36 printf("%.4f\n", fun(l)); 37 } 38 }
时间: 2024-10-12 08:52:36