[COMP2119] Searching - Building and Egg Problem

Description

There is a building with n floors and you have m eggs. Determine the lowest floor thrown from which an egg will break. If an egg is broken, it cannot be used again, otherwise, it can.

 

What is the minimum number of probes needed?

Let fi,j be the minimum probe needed for i floors and j eggs. Consider the first floor to search, denote it as k, then

fi,j=1+mink(max(fk1,j1,fik,j))

where the first term stands for the case that the egg is broken, and the second for not broken.

The answer should be fn,m. The initial conditions are

f0,j=0

fi,1=i

Code:

复制代码
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) f[i][j]=2147483647;
for (int i=1;i<=n;i++){
    f[i][1]=i;
    for (int j=2;j<=m;j++){
        for (int k=1;k<=i;k++)
            if (1+max(f[k-1][j-1],f[i-k][j])<f[i][j]){  //记得初始化
                f[i][j]=1+max(f[k-1][j-1],f[i-k][j]);
                fir[i][j]=k;
            }
    }
}
printf("%d",f[n][m]);
复制代码

 

What are the optimal strategy and the asymptotic evaluation of the minimum number of probes?

Case 1, m=1: The only way is to do O(n) linear search from lower to higher since there is only 1 egg.

Case 2, m>log2n: O(logn) binary search. (not proven to be the optimal strategy but is consistent with the numerical result)

Case 3, 1<m<log2n:

Let T(n,m) be the number of probes the optimal strategy takes.

First, consider m=2, i.e. try to find T(n,2).

Square search is a feasible strategy, so T(n,2)=O(n).

Now, try to find the lower bound of T(n,2):

Assume floors the optimal strategy A probes forms an increasing sequence a1,a2,,ak.

According to the above upper bound, kn.

  If k<n: we have the following deduction

i=1k1(ai+1ai1)ni,(ai+1ai1)nk1nn1nn=n

  This means, there exists one gap between two consecutive probes, say ak and ak+1, that is large than (n). Then if we

  create a situation where the critical floor is in this gap, the ak+1 probe will break one egg. So for the remaining n floors in

  between, we can only do a linear search since there is only one egg left. Then the total number of probes will be larger than

  n, which contradicts the assumption.

Therefore, T(n,2)=Ω(n)

The upper bound and lower bound match, meaning that T(n,2)=Θ(n).

With m=2 being the base case, we can do a two-variable mathematical induction on T(n,k) with the following induction formula (which is the same as the DP):

T(n,m)=1+mini(max(T(i1,m1),T(ni,m)))

The conclusion is that T(n,m)=O(n1m) for 1<m<log2n. But actually it doesn't match that well with the numerical result... 

 

posted @   Maaaaax  阅读(31)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示