Editorial for Yet Another Contest 7 P5 - Egg Dropping
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
We can only drop at most eggs, so each egg variety can be dropped at most times on average. We therefore need to implement a variant of binary search. Simply binary searching for each egg variety individually is sufficient to score of the points. In order to reduce the number of moves, we should use parallel binary search. The following two strategies are based around different methods of implementing parallel binary search:
Strategy 1: Divide and Conquer
Consider a function which takes in a contiguous range of floors and a list of egg varieties corresponding to them in some order, and assigns each egg variety to a floor. Then:
- If there is only one floor, then the only egg variety must correspond to this floor.
- Otherwise, drop each egg variety from the floor . The egg varieties that shattered must correspond to the floors , whilst the remaining eggs must correspond to the floors . We can recursively solve each half.
Naively implemented, this solution scores about of the points. We can further apply the following optimizations:
- When solving for a group of egg varieties and a range of floors, if we've already found all egg varieties that will shatter, or all eggs that will not shatter, then we can short-circuit and avoid dropping the rest of the eggs since we can deduce whether they will shatter.
- We can choose the order in which we can recurse on each half in order to minimize the number of turn-arounds.
- We can choose any value of in the range , rather than exactly in the middle, sacrificing extra egg drops for fewer moves and turn-arounds. The optimal value of can be computed using dynamic programming. The exact details are left as an exercise to the reader.
A well implemented solution can score of the points.
Strategy 2: Sweep
The previous strategy had the disadvantage that it required many turn-arounds. We should therefore look for an implementation of parallel binary search that doesn't require many direction changes:
- For each egg variety , we maintain variables and , meaning that we know that the answer for this egg variety is in the range .
- Then, while there exists at least one egg variety with , we compute for each where .
- We process the egg varieties by increasing order of , dropping the -th egg variety at floor . We can adjust the values of and as in a traditional binary search.
This strategy allows us to halve the range of every egg variety in a single sweep, without changing direction. Naively implemented, this solution scores about of the points. We can further apply the following optimizations:
- In order to minimise movements and direction changes, we should alternate between processing egg varieties by increasing order of and decreasing order of . This is already sufficient to score of the points.
- We can apply the same short-circuiting optimization as in Strategy .
- Suppose we are processing the egg varieties by increasing order of . If we ever set , we can recalculate and allow this egg variety to be processed in the same sweep. We can do something similar when processing the egg varieties by decreasing order of .
- Once again, we can choose the value of to be anything in the range . The optimal value of can be computed using dynamic programming, although this is less straightforward; it helps to approximate the total number of moves as number of sweeps. The exact details are left as an exercise to the reader.
This strategy can score all of the points.
Comments