【二分】三种基本模板
作者:
Zcrew
,
2024-04-03 21:08:29
,
所有人可见
,
阅读 19
#define 1e-8 eps
bool check(int/double x){
...
for(...) ...
if(...) return true/false;
return false/true;
}
int bs_1(int l, int r){
while(l < r){
int mid = l + r >> 1;
if(check(mid)) r = mid;
else l = mid + 1;
}
return l;
}
int bs_2(int l, int r){
while(l < r){
int mid = l + r + 1 >> 1;
if(check(mid)) l = mid;
else r = mid - 1;
}
return l;
}
int bs_3(double l, double r){
while(r - l > eps){
int mid = (l + r) / 2;
if(check(mid)) l = mid;
else r = mid;
}
return l;
}