Three-point search algorithm (reprint learning) * "Template"

Source: Internet
Author: User
Tags acos cos

Reprint Address: http://blog.csdn.net/acdreamers/article/details/9989197

The first thing to say is the concept of three points:

The two points are divided into two segments of equal length, and three points are divided into three segments of equal length, to find, so that the search is called a three-point search, three points to find a pass often used to quickly determine the maximum value. As we all know, the requirements of the binary algorithm is that the search sequence is a monotone sequence, while the three-method-oriented search sequence requires: The sequence is a convex function. similar to the dichotomy method, the three-part algorithm first divides the interval into three segments of equal length, then there are two points between L and R: ll=l+ (r-l)/3= (2L+R)/3 and rr=r-(r-l)/3= (L+2R)/3 if LL is closer to the maximum than RR, we discard the right interval, otherwise we discard the left interval. correctness of the algorithm: 1, LL and RR on the same side of the maximum value. Since the convexity function is monotonic on either side of the maximum (minimum), LL and RR are larger The number of (small) is naturally closer to the maximum value. At this point, the interval that we are far from the most value cannot contain the maximum value, so we can discard it. 2, LL and RR on both sides of the maximum value. Since the maximum value is in the middle of an interval, after we discard a range, it does not affect the maximum value. Typical topic: hdu4355,hdu2438,poj3301 title: party All the time [CPP]View Plaincopy
  1. #include <iostream>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <math.h>
  5. Using namespace std;
  6. Const Double eps=1e-4;
  7. const int n=50010;
  8. Double P[n],w[n];
  9. int n;
  10. Double equ (double x)
  11. {
  12. double ans=0;
  13. For (int i=0;i<n;i++)
  14. {
  15. double S=fabs (p[i]-x);
  16. ans+=w[i]*s*s*s;
  17. }
  18. return ans;
  19. }
  20. Double Ternarysearch (double L,double R)
  21. {
  22. While (r-l>eps)
  23. {
  24. double ll= (2*l+r)/3;
  25. double rr= (l+2*r)/3;
  26. double ans1=equ (LL);
  27. double ans2=equ (RR);
  28. if (ans1>ans2)
  29. L=ll;
  30. Else
  31. R=RR;
  32. }
  33. return L;
  34. }
  35. int main ()
  36. {
  37. int t,i;
  38. scanf ("%d", &t);
  39. For (int t=1;t<=t;t++)
  40. {
  41. scanf ("%d", &n);
  42. double l=1000000;
  43. double r=-1000000;
  44. For (i=0;i<n;i++)
  45. {
  46. scanf ("%lf%lf", &p[i],&w[i]);
  47. if (p[i]<l) l=p[i];
  48. if (p[i]>r) r=p[i];
  49. }
  50. double Tmp=ternarysearch (l,r);
  51. printf ("Case #%d:%.0lf\n", t,equ (TMP));
  52. }
  53. return 0;
  54. }
title: Turn the corner Test Instructions: Given the width of two roads at a right angle, then give the length and width of the car, and ask if the car can pass the curve? such as: to make the car turn this way, the left side of the car is as far as the right-angled point, and the car's bottom bottom is as far as the bottom side. we establish a Cartesian coordinate system with the O point as the origin, and we can give a function f (a) of the P-point horizontal axis according to angle a . then it's easy to get: There are conditions:, it can be easily proved that a single-peak function, so next is the three points, if the maximum value is less than or equal to y, then you can pass this right-angled corner, otherwise it won't work. [CPP]View Plaincopy
  1. #include <iostream>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <math.h>
  5. Using namespace std;
  6. Const Double eps=1e-8;
  7. Double x,y,l,w;
  8. Double equ (double t)
  9. {
  10. return L*cos (t) + (W-x*cos (t))/sin (t);
  11. }
  12. Double Ternarysearch (double L,double R)
  13. {
  14. While (r-l>eps)
  15. {
  16. double ll= (2*l+r)/3;
  17. double rr= (l+2*r)/3;
  18. double ans1=equ (LL);
  19. double ans2=equ (RR);
  20. if (ans1<ans2)
  21. L=ll;
  22. Else
  23. R=RR;
  24. }
  25. return L;
  26. }
  27. int main ()
  28. {
  29. while (~scanf ("%lf%lf%lf%lf", &x,&y,&l,&w))
  30. {
  31. Double Low=0,high=acos (-1.0)/2;
  32. double Tmp=ternarysearch (Low,high);
  33. if (EQU (TMP) <=y) puts ("yes");
  34. else puts ("no");
  35. }
  36. return 0;
  37. }

title: Texas Trip Test Instructions: The coordinates of a given n point on a plane, and the area of the smallest square containing the n points is calculated. Analysis: We first find a small side length is parallel to the axis of the smallest square, and then we only need at 0-90 degrees all point rotation process, each rotation a little Compare, and then record the edge length of the smallest square, and in this rotation process of the square side length about the rotation angle function is a single-peak function, so you can three points. Here you know the coordinate rotation formula: if the coordinates before the rotation are rotated, then there are: [CPP]View Plaincopy
    1. #include <iostream>
    2. #include <string.h>
    3. #include <stdio.h>
    4. #include <math.h>
    5. Using namespace std;
    6. Const Double eps=1e-12;
    7. Const Double Pi=acos (-1.0);
    8. Const Double inf=1e6;
    9. const int n=35;
    10. Double X[n],y[n];
    11. int n;
    12. Double Tri (double t)
    13. {
    14. double tx,ty;
    15. double Bx,by,sx,sy;
    16. Bx=by=-inf;
    17. Sx=sy=inf;
    18. For (int i=0; i<n; i++)
    19. {
    20. Tx=x[i]*cos (t)-y[i]*sin (t);
    21. Ty=x[i]*sin (t) +y[i]*cos (t);
    22. //Rotate the coordinate system to find out where the rotated point should be
    23. Bx=max (TX,BX);
    24. Sx=min (TX,SX);
    25. By=max (Ty,by);
    26. Sy=min (Ty,sy);
    27. }
    28. return Max (Bx-sx,by-sy);
    29. }
    30. Double Ternarysearch (double L,double R)
    31. {
    32. While (r-l>eps)
    33. {
    34. double ll= (2*l+r)/3;
    35. double rr= (l+2*r)/3;
    36. double Ans1=tri (LL);
    37. double Ans2=tri (RR);
    38. if (ans1>ans2)
    39. L=ll;
    40. Else
    41. R=RR;
    42. }
    43. return L;
    44. }
    45. int main ()
    46. {
    47. int t;
    48. scanf ("%d", &t);
    49. While (t--)
    50. {
    51. scanf ("%d", &n);
    52. For (int i=0;i<n;i++)
    53. scanf ("%lf%lf", &x[i],&y[i]);
    54. double LOW=0,HIGH=PI/2;
    55. double Tmp=ternarysearch (Low,high);
    56. printf ("%.2lf\n", Tri (TMP) *tri (TMP));
    57. }
    58. return 0;
    59. }

Three-point search algorithm (reprint learning) * "Template"

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: info-contact@alibabacloud.com 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.