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 $f_{i,j}$ be the minimum probe needed for $i$ floors and $j$ eggs. Consider the first floor to search, denote it as $k$, then
$$f_{i,j}=1+\underset{k}{min}(max(f_{k-1,j-1},f_{i-k,j}))$$
where the first term stands for the case that the egg is broken, and the second for not broken.
The answer should be $f_{n,m}$. The initial conditions are
$$f_{0,j}=0$$
$$f_{i,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>log_2n$: $O(logn)$ binary search. (not proven to be the optimal strategy but is consistent with the numerical result)
Case 3, $1<m<log_2n$:
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(\sqrt{n})$.
Now, try to find the lower bound of $T(n,2)$:
Assume floors the optimal strategy $A$ probes forms an increasing sequence $a_1,a_2,\cdots,a_k$.
According to the above upper bound, $k \leq n$.
If $k < \sqrt{n}$: we have the following deduction
$$\sum_{i=1}^{k-1}(a_{i+1}-a_i-1) \leq n \Rightarrow \exists i,(a_{i+1}-a_i-1) \geq \frac{n}{k-1} \geq \frac{n}{\sqrt{n}-1} \geq \frac{n}{\sqrt{n}} = \sqrt{n}$$
This means, there exists one gap between two consecutive probes, say $a_k$ and $a_{k+1}$, that is large than $\sqrt(n)$. Then if we
create a situation where the critical floor is in this gap, the $a_{k+1}$ probe will break one egg. So for the remaining $\sqrt{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
$\sqrt{n}$, which contradicts the assumption.
Therefore, $T(n,2)=\Omega(\sqrt{n})$
The upper bound and lower bound match, meaning that $T(n,2)=\Theta(\sqrt{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+\underset{i}{min}(max(T(i-1,m-1),T(n-i,m)))$$
The conclusion is that $T(n,m)=O(n^{\frac{1}{m}})$ for $1 < m < log_2n$. But actually it doesn't match that well with the numerical result...
标签:COMP2119,Building,search,sqrt,strategy,only,Problem,egg,probes From: https://www.cnblogs.com/ms-qwq/p/16840239.html