二分的写法

求最小值

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; 
    }
posted @ 2020-08-24 19:11  Luglucky  阅读(192)  评论(0编辑  收藏  举报