Good food, now three points of scholarship.
Three points: finds the maximum (minimum) value of a convex (concave) function.
Template 1:
Doublethree_divide () {DoubleL=0, r= +; DoubleMid= (L+R)/2; DoubleMm= (L+mid)/2; while(r-l>=EPS) { if(F (mid) >f (mm)) r=mid; ElseL=mm; Mid= (l+r)/2; MM= (L+mid)/2; } returnF (mid);}
Template 2:
Doublethree_divide () {DoubleL=0, r= +; Doublem1= (L+L+R)/3; DoubleM2= (L+R+R)/3; while(r-l>=EPS) { if(F (M2) >f (M1)) r=m2; ElseL=L1; M1= (l+l+r)/3; M2= (l+r+r)/3; } returnF (M1);}
Example 1:hdu 3714 Error Curves
Code:
#include <iostream>#include<cmath>#include<cstring>#include<algorithm>#include<iomanip>using namespacestd;#definell Long Long#definePB Push_back#defineMem (A, B) memset (A,b,sizeof (a))Const Doubleeps=1e-9;Const intn=1e4+5;structnode{DoubleA,b,c;} X[n];intN;DoubleFDoublet) { Doubleans=t*t*x[0].a+t*x[0].b+x[0].c; for(intI=1; i<n;i++) ans=max (ans,t*t*x[i].a+t*x[i].b+x[i].c); returnans;}Doublethree_divide () {DoubleL=0, r= +; Doublem1= (L+L+R)/3; DoubleM2= (L+R+R)/3; while(r-l>=EPS) { if(F (M2) >f (M1)) r=m2; ElseL=M1; M1= (l+l+r)/3; M2= (l+r+r)/3; } returnF (M1);}intMain () {Ios::sync_with_stdio (false); Cin.tie (0); intT; CIN>>T; while(t--) { Doubleans=-0x3f3f3f3f; CIN>>N; for(intI=0; i<n;i++) cin>>x[i].a>>x[i].b>>x[i].c; Ans=three_divide (); cout<<fixed<<setprecision (4) <<ans<<Endl; } return 0;}
Algorithm notes--three points to find