• Welcome to the world's largest Chinese hacker forum

    Welcome to the world's largest Chinese hacker forum, our forum registration is open! You can now register for technical communication with us, this is a free and open to the world of the BBS, we founded the purpose for the study of network security, please don't release business of black/grey, or on the BBS posts, to seek help hacker if violations, we will permanently frozen your IP and account, thank you for your cooperation. Hacker attack and defense cracking or network Security

    business please click here: Creation Security  From CNHACKTEAM

Recommended Posts

二分

整数二分

二分搜索法要寻找一个时间复杂度为logn的元素

原理:每次将当前搜索的区间长度减半。

取当前间隔的中点。

像属性一样,然后更新间隔

如果l和r重合,则搜索完成。

//第一种模板

while(左向右)

{

int mid=l r 1 1

if(check(mid))l=mid;

else r=mid-1;

}

//第二个模板

while(左向右)

{

int mid=l r 1;

if(check(mid))r=mid;

else l=mid 1;

}

为什么会有两个模板?

当l=mid时,如果区间为l=r-1,则区间会更新为l=r-1,因为l r 1向下取整。

同样的,当r=mid,当r=l 1,如果mid是l r 1 1,它将被更新为l=r 1

所以如果混用会出现死循环现象.

浮点数二分

二进制浮点数比较简单,因为不需要考虑无限循环,只需要定义一个eps来表示题目要求的精度。

const double EPS=1e-6;

白色(右侧左侧eps)

{

double mid=(l r)/2;

if(check(mid))r=mid;

else l=mid

}

Link to comment
Share on other sites