[Algorithm analysis]-three points search

The binary search is generally used to find the critical value that just satisfies a certain condition, and the search interval is required to meet the monotonicity, while the three-section search is generally to find the extreme value on the convex function or the concave function.

principle

The image is assumed that the interval function [lo,hi]is upwardly open curve, then there must be a maximum value within the interval, Qusan Fen point m1and m2, if f(m1)>f(m2), then the maximum is located [lo,m2]within the exclusion zone [m2,hi]; conversely maxima in regions [m1,hi]within, may be excluded [lo,m1], the time The complexity is O(logn).

Instance

Cone of Mr. B

Problem surface : Given that the surface area of ​​a cone is S, find the maximum value of its volume V.

Analysis : Set the radius of the cone to r. Since the surface area S is fixed, intuitively, when r is very small, the cone is high, but too thin, resulting in small volume; when r is large, the cone is fat, but too short and not large; Appropriate r value should be used to balance the width and height to achieve the maximum volume.

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
int s;
double getl(double r) {
    
    
    return (s - pi * r * r) / pi / r;
}
double geth(double r) {
    
    
    double l = getl(r);
    return sqrt(l * l - r * r);
}
double vol(double r) {
    
    
    double h = geth(r);
    return pi * r * r * h / 3;
}
int main() {
    
    
    cin >> s;
    double lo = 0, hi = sqrt(s/2/pi);
    while (hi - lo > 1e-6) {
    
    
        double m1 = lo + (hi - lo) / 3;
        double m2 = lo + (hi - lo) / 3 * 2;
        double v1 = vol(m1), v2 = vol(m2);
        if (v1 > v2)
            hi = m2;
        else
            lo = m1;
    }
    printf("%.6f\n", vol(lo));
    return 0;
}

Cow party

Problem surface : There are n points on the x-axis. Given the coordinate x[i] and weight w[i] of the i-th point, you need to select a point d on the x-axis to make it the sum(w[i]*|d-x[i]|^3)smallest, and find the minimum.

Analysis: If d is too left or right, the result will be too large. It should be taken somewhere in the middle to balance the weighted distance on the left and right sides to obtain the minimum value.

#include <bits/stdc++.h>
using namespace std;
int n, T;
double x[50005], w[50005];
double f(double d) {
    
    
    double ret = 0;
    for (int i = 1; i <= n; i++) {
    
    
        double u = fabs(d - x[i]);
        ret += u * u * u * w[i];
    }
    return ret;
}
int main() {
    
    
    cin >> T;
    for (int z = 1; z <= T; z++) {
    
    
        double lo = 1e6, hi = -1e6, ans = 9e18;
        cin >> n;
        for (int i = 1; i <= n; i++) {
    
    
            cin >> x[i] >> w[i];
            lo = min(lo, x[i]);
            hi = max(hi, x[i]);
        }
        if (n == 1) {
    
    
            ans = 0;
        } else {
    
    
            while (hi - lo > 1e-6) {
    
    
                double m1 = lo + (hi - lo) / 3;
                double m2 = lo + (hi - lo) / 3 * 2;
                double v1 = f(m1), v2 = f(m2);
                if (v1 < v2)
                    hi = m2, ans = v1;
                else
                    lo = m1, ans = v2;
            }
        }
        printf("Case #%d: %d\n", z, int(ans + 0.5));
    }
    return 0;
}

Search within integers

The above two examples both perform a three-point search within the real number range, and stop when the length of the search interval is less than the specified precision.

If it is required that the value must be an integer, the three-point search can also work. The difference is the processing when the loop is terminated: the search is stopped when it lo+2is equal hi, and at this time m1=m2, it lo,m1,hiis three adjacent integers, and the function at these three points is calculated separately Take the extreme value of the three.

Resource portal

  • Pay attention to [ Be a tender program ape ] public account
  • Reply to the [python information] [2020 Autumn Recruitment] in the background of [ Be a Tender Program Ape ] public account, and you can get the corresponding surprise!

「❤️ Thank you everyone」

  • Like to support it, so that more people can see this content (If you don’t like it, it’s all hooligans-_-)
  • Welcome to share your thoughts with me in the message area, and you are also welcome to record your thought process in the message area.

Guess you like

Origin blog.csdn.net/ywsydwsbn/article/details/109245042