每日一題 day38 打卡node
這道題運用的是三分,就是說具備必定的單調性,找最大最小值,而後和二分基本相似,就是說特性就是說當前兩個點比較,較優的點和最優勢在相對了較差點的同側,就是說那邊差就把範圍定到那裏去ios
注意:要注意精度問題,通常定到1e-9或1e-11git
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #define int long long 6 #define maxn 100000+10 7 #define INF 0x7fffffff 8 #define rep(i,s,e) for(register int i=s;i<=e;++i) 9 using namespace std; 10 inline int read() 11 { 12 int x=0; 13 bool f=1; 14 char c=getchar(); 15 for(; !isdigit(c); c=getchar()) if(c=='-') f=0; 16 for(; isdigit(c); c=getchar()) x=(x<<3)+(x<<1)+c-'0'; 17 if(f) return x; 18 return 0-x; 19 } 20 int T,n; 21 struct node 22 { 23 double a,b,c; 24 }x[maxn]; 25 inline double max(double x,double y) 26 { 27 if(x>=y) return x; 28 return y; 29 } 30 inline double calc(double xx) 31 { 32 double res=-INF; 33 rep(i,1,n) 34 res=max(res,x[i].a*xx*xx+x[i].b*xx+x[i].c); 35 return res; 36 } 37 signed main() 38 { 39 T=read(); 40 while(T--) 41 { 42 memset(x,0,sizeof(x)); 43 n=read(); 44 rep(i,1,n) x[i].a=read(),x[i].b=read(),x[i].c=read(); 45 double l=0.0,r=1000.0; 46 while(r-l>=1e-11) 47 { 48 double lmid=l+(r-l)/3,rmid=r-(r-l)/3; 49 if(calc(lmid)<=calc(rmid)) r=rmid; 50 else if(calc(lmid)>calc(rmid)) l=lmid; 51 } 52 printf("%.4lf\n",calc(l)); 53 } 54 return 0; 55 }
請各位大佬斧正(反正我不認識斧正是什麼意思)spa