Editorial for Yet Another Contest 7 P5 - Egg Dropping


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: Josh

We can only drop at most 2×105 eggs, so each egg variety can be dropped at most 2×105104=20 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 5% 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 [low,high] 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 mid=low+high2. The egg varieties that shattered must correspond to the floors [low,mid], whilst the remaining eggs must correspond to the floors [mid+1,high]. We can recursively solve each half.

Naively implemented, this solution scores about 23% 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 mid in the range [low,high), rather than exactly in the middle, sacrificing extra egg drops for fewer moves and turn-arounds. The optimal value of mid can be computed using dynamic programming. The exact details are left as an exercise to the reader.

A well implemented solution can score 92% 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 i, we maintain variables lowi and highi, meaning that we know that the answer for this egg variety is in the range [lowi,highi].
  • Then, while there exists at least one egg variety with lowihighi, we compute midi=lowi+highi2 for each i where lowihighi.
  • We process the egg varieties by increasing order of midi, dropping the i-th egg variety at floor midi. We can adjust the values of lowi and highi 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 14% 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 midi and decreasing order of midi. This is already sufficient to score 36% of the points.
  • We can apply the same short-circuiting optimization as in Strategy 1.
  • Suppose we are processing the egg varieties by increasing order of midi. If we ever set lowi=midi+1, we can recalculate midi 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 midi.
  • Once again, we can choose the value of midi to be anything in the range [lowi,highi). The optimal value of midi can be computed using dynamic programming, although this is less straightforward; it helps to approximate the total number of moves as N×(number of sweeps+1). The exact details are left as an exercise to the reader.

This strategy can score all 100% of the points.


Comments

There are no comments at the moment.