C++ アルゴリズムとデータ構造のライブラリ
#include "library/search/binary_search/binary_search_real.hpp"3.5<=xならL=3.5, R=3.5(LRの誤差がEPS内)
L R
x x x o o o o
↑ここを求める
$O(logN)$
auto [L, R] = binary_search_real([&](double x){
return 3.5 <= x;
});
#pragma once
#include <functional>
const long double EPS = 1e-14;
pair<double, double> binary_search_real(function<bool(double)> f) {
double L = 0, R = 1, MID = 0;
while (!f(R)) R *= 2;
auto ABS = [&]() { return abs(R - L) > EPS; };
auto REL = [&]() { return abs(R - L) / max(R, L) > EPS; };
while (ABS() and REL()) {
MID = L + (R - L) / 2;
(f(MID) ? R : L) = MID;
}
return {L, R};
}#line 2 "library/search/binary_search/binary_search_real.hpp"
#include <functional>
const long double EPS = 1e-14;
pair<double, double> binary_search_real(function<bool(double)> f) {
double L = 0, R = 1, MID = 0;
while (!f(R)) R *= 2;
auto ABS = [&]() { return abs(R - L) > EPS; };
auto REL = [&]() { return abs(R - L) / max(R, L) > EPS; };
while (ABS() and REL()) {
MID = L + (R - L) / 2;
(f(MID) ? R : L) = MID;
}
return {L, R};
}