题目大意:给定n个二次函数fi(x),规定F(x) = max(fi(x)),求F在[0,1000]上的max
其实F(x)也是一个单峰函数,因为如果出现双峰,显然有一个可以继续往上走把另一个峰遮住,即另一个峰不可能是max
三分即可
被精度卡了半天
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <algorithm> 6 #include <queue> 7 #include <vector> 8 #include <map> 9 #include <string> 10 #include <cmath> 11 #define min(a, b) ((a) < (b) ? (a) : (b)) 12 #define max(a, b) ((a) > (b) ? (a) : (b)) 13 #define abs(a) ((a) < 0 ? (-1 * (a)) : (a)) 14 template<class T> 15 inline void swap(T &a, T &b) 16 { 17 T tmp = a;a = b;b = tmp; 18 } 19 inline void read(int &x) 20 { 21 x = 0;char ch = getchar(), c = ch; 22 while(ch < '0' || ch > '9') c = ch, ch = getchar(); 23 while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar(); 24 if(c == '-') x = -x; 25 } 26 27 const double INF = 0x3f3f3f3f; 28 const double eps = 0.0000000001; 29 30 31 int t,n,a[10100],b[10100],c[10010]; 32 33 double f(double x) 34 { 35 double ma = x * x * a[1] + x * b[1] + c[1], t; 36 for(int i = 2;i <= n;++ i) 37 { 38 t = x * x * a[i] + x * b[i] + c[i]; 39 //if(!cmp(t, ma)) ma = t; 40 ma = max(ma, t); 41 } 42 return ma; 43 } 44 45 int main() 46 { 47 read(t); 48 for(;t;--t) 49 { 50 read(n); 51 for(int i = 1;i <= n;++ i) read(a[i]), read(b[i]), read(c[i]); 52 double l = 0, r = 1000, ans, mid1, mid2; 53 while(r - l - eps >= 0) 54 { 55 mid1 = l + (r - l) / 3.0;mid2 = r - (r - l) / 3.0; 56 if(f(mid1) - f(mid2) >= 0) l = mid1; 57 else r = mid2; 58 } 59 printf("%.4lf ", f(l)); 60 } 61 return 0; 62 }