转自 https://www.cnblogs.com/evasean/p/7208986.html
题目原文:
Suppose that you have an n-story building (with floors 1 through n) and plenty of eggs. An egg breaks if it is dropped from floor T or higher and does not break otherwise. Your goal is to devise a strategy to determine the value of T given the following limitations on the number of eggs and tosses:
Version 0: 1 egg, ≤T tosses.
Version 1: ∼1lgn eggs and ∼1lgn tosses.
Version 2: ∼lgT eggs and ∼2lgT tosses.
Version 3: 2 eggs and ∼2 n \sqrt{n} n tosses.
Version 4: 2 eggs and ≤c T \sqrt{T} Ttosses for some fixed constant c
分析:
version0 : 拿着一个鸡蛋从1~n依次扔就可以,到floor T会碎,故复杂度为≤T
version 1: 采用二分查找,首先从n/2层开始扔:
if(鸡蛋碎) 从(n/2)/2层开始扔;
else 从n/2+(n/2)/2层开始扔
二分方法需要lgn个鸡蛋尝试lgn次
version 2: 依次从1, 2, 4, 8, 16, 32,…2k开始扔,如果鸡蛋在2k碎了,那么2k-1≤T≤2k,这时已经使用了 lgT 次步,接下来在[2k-1+1,2k)区间进行version1的二分查找方法,需要花费lgT步。这两种操作加起来总共花费2lgT步
version 3: 将0~n层楼分成[1, n \sqrt{n} n-1], [ n \sqrt{n} n, 2 n \sqrt{n} n-1], [2 n \sqrt{n} n,3 n \sqrt{n} n