cp-library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub rniya/cp-library

:warning: Golden Section Search
(src/optimization/golden_section_search.hpp)

Required by

Code

#include <cassert>
#include <functional>
#include <utility>

template <typename T, bool get_min = true>
std::pair<long long, T> golden_section_search(const std::function<T(long long)>& f, long long mini, long long maxi) {
    assert(mini <= maxi);
    long long a = mini - 1, x, b;
    {
        long long s = 1, t = 2;
        while (t < maxi - mini + 2) std::swap(s += t, t);
        x = a + t - s, b = a + t;
    }
    T fx = f(x), fy;
    while (a + b != 2 * x) {
        long long y = a + b - x;
        fy = f(y);
        if (maxi < y or (get_min ? fx < fy : fx > fy)) {
            b = a;
            a = y;
        } else {
            a = x;
            x = y;
            fx = fy;
        }
    }
    return {x, fx};
}
#line 1 "src/optimization/golden_section_search.hpp"
#include <cassert>
#include <functional>
#include <utility>

template <typename T, bool get_min = true>
std::pair<long long, T> golden_section_search(const std::function<T(long long)>& f, long long mini, long long maxi) {
    assert(mini <= maxi);
    long long a = mini - 1, x, b;
    {
        long long s = 1, t = 2;
        while (t < maxi - mini + 2) std::swap(s += t, t);
        x = a + t - s, b = a + t;
    }
    T fx = f(x), fy;
    while (a + b != 2 * x) {
        long long y = a + b - x;
        fy = f(y);
        if (maxi < y or (get_min ? fx < fy : fx > fy)) {
            b = a;
            a = y;
        } else {
            a = x;
            x = y;
            fx = fy;
        }
    }
    return {x, fx};
}
Back to top page