[英]How to optimize an algorithm that returns the value of a positive integer x such that f(x) >=0 for a partially known function?
The only things you know about this function are that 你对这个功能的唯一了解就是
(i) x >= 1 for f(x) such that f(x) >= 0. (i)对于f(x),x> = 1,使得f(x)> = 0。
(ii) It is strictly increasing when x > 0. (ii)当x> 0时严格增加。
(iii) Evaluating the function at any point takes constant time. (iii)在任何时候评估功能需要恒定的时间。
How do you return the value of a positive integer x such that f(x) >=0 in < O(n) time, preferably in O(log(n)) time? 如何在<O(n)时间内返回正整数x的值,使得f(x)> = 0,最好是在O(log(n))时间内?
In O(n) time, you just have to keep querying the algorithm for all positive integers until it hits f(x) >= 0. 在O(n)时间内,您只需要继续查询所有正整数的算法,直到它达到f(x)> = 0。
Evaluate f(x) for x powers of 2, until you find f(2^n) < 0 and f(2^(n+1)) >= 0. Then bisect until you find the lowest value. 评估f(x)的x次幂为2,直到找到f(2 ^ n)<0且f(2 ^(n + 1))> = 0.然后平分直到找到最低值。 It's O(log n) time to find the first power of 2 for which the function is non-negative, and then O(log n) to binary search in the range 2^n to 2^(n+1).
找到函数为非负的2的第一个幂的O(log n)时间,然后在2 ^ n到2 ^(n + 1)的范围内找到O(log n)到二进制搜索。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.