二分的写法
求最小值
while (l<r) {
int mid= (l+r)>>1;
if (check (mid)) r=mid;
else l=mid+1;
}
求最大值
while (l<r) {
int mid= (l+r+1)>>1;
if (check (mid)) l=mid;
else r=mid-1;
}
在正数序列里面的二分
int find (int num,int l,int r) {
int mid;
while (l<=r) {
mid= (l+r)>>1;
if (num>=b[mid]) l=mid+1;
else r=mid-1;
}
return l;
}
浮点二分答案
while ((r-l)>1e-3) {
double mid=(l+r)/2;
if (check (mid)) l=mid;
else r=mid;
}