bool cheek(int x){
}
//区间[l,r]被划分成[l,mid]和[mid + 1,r]的时候使用
int bsearch_1(int left,int right) {
while (left < right) {
int mid = left + right >>1;
if (cheek(mid)) {
right = mid;
} else {
mid = left + 1;
}
}
return left;
}
//区间[l.r]被划分为[left,mid - 1]和[mid,right]
int bsarch_2(int left,int r) {
while (left < right) {
int mid = left + right + 1>>1;//注意这里要加1
if (cheek(mid)) {
left = mid;
} else {
right = mid - 1;
}
}
return left;
}
整数二分算法模版
最新推荐文章于 2024-11-03 17:23:52 发布