下界二进制搜索?
Under bound binary search?
如何在常规二进制搜索中设置我的条件,以便找到最大值 x 使得 f(x) <= t 对于某个临界值 t?而不是意外返回最低的数字 > t.
现在我的界限是
if f(x) > t then high = x-1
else if f(x)< t then low = x+1
else return x
主要 while 循环是 while low <= high
这个怎么样?
int ans = -1;
bsearch()
if f(x) > t then high = x-1
else if f(x)<= t then low = x+1, ans = x
另一种方法:
只需使用您当前的 bsearch 找到 f(x) 最小值 > t 的 x,
那么你想要的只是 x - 1 吗? (如果存在)
PS:如果你用的是C++,你可以用upper_bound()求出x的位置,那么x-1就是你的答案
如何在常规二进制搜索中设置我的条件,以便找到最大值 x 使得 f(x) <= t 对于某个临界值 t?而不是意外返回最低的数字 > t.
现在我的界限是
if f(x) > t then high = x-1
else if f(x)< t then low = x+1
else return x
主要 while 循环是 while low <= high
这个怎么样?
int ans = -1;
bsearch()
if f(x) > t then high = x-1
else if f(x)<= t then low = x+1, ans = x
另一种方法:
只需使用您当前的 bsearch 找到 f(x) 最小值 > t 的 x, 那么你想要的只是 x - 1 吗? (如果存在)
PS:如果你用的是C++,你可以用upper_bound()求出x的位置,那么x-1就是你的答案