Algorithm notes--three points to find

Source: Internet
Author: User

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


#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.