Editorial for WC '15 Contest 1 S2 - Wildcat Wildcards


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.

This may have been a deceptively simple problem to many competitors. After reading it, you may even believe that you could solve all valid inputs by hand, since there are only 11 numbers to deal with. We want to optimally assign the 3 types of wildcards so that the minimum quantity across all of the 8 letter cards is maximized. In the cases worth 10\% of marks with no wildcards to assign, this is as simple as finding the minimum of all of the quantities (the number of signs we can make is limited by the minimum of each quantity, since the letters are distinct).

One intuitive way to approach this problem is by repeatedly taking the smallest quantity across all the 8 letter cards, and trying to raise it to the second smallest quantity across the 8 cards through assigning wildcards. We prioritize taking vowel and consonant specific wildcards first, because those are more limiting. When we have no more letters to distribute, we go through all of them and take the minimum value. The catch is when we end up with two or more cards all having the smallest quantity. Then, we would have to resort to some trickier decisions and casework. Going down this path, we'll probably reach a point where our code becomes much more complicated than what we bargained for.

To obtain a simpler solution, we can start by pointing out a rather obvious fact that if it's possible to make N signs, then it's also possible to make any number of signs less than N (but not greater). Rather than trying to answer the question "what is the max number of signs we can make?", note that it is much easier to answer the question "can we make N signs?" The latter question is answered by simply assigning all available wildcards to try to make every quantity equal to N. Once again, we should assign vowel and consonant wildcards before the generic wildcards. Since there are only 11 types of cards overall, we can safely assume that this "check" operation has \mathcal O(1) running time.

A good (but not good enough) way to move forward from here is to loop N upwards starting from 0, at each iteration asking ourselves whether N signs could be made using the wildcard assignment algorithm we've discussed above. Immediately when we reach an N for which we find N signs cannot be made, output N-1 (the largest possible number for which that number of signs could be made). This is a good start, but since the answer can be over 10^8 (100 million), it is not good enough to pass in time for all of the test cases (a good rule of thumb is 60 million steps per second of time limit).

To solve the problem completely, we can use binary search. Binary search is classically used for finding the position of a number in a sorted array, but it can be used just as well in these discrete situations. In this case, we have a boolean function which returns true up to a certain point, and false for everything after. Binary search can be used to pinpoint the exact position where a function switches from true to false in \mathcal O(\log N) time, where N is the search space. This article is a good explanation of discrete binary searching. By binary searching on the value of N with our boolean function being whether we can make N signs, we will be able to solve the problem in a very efficient manner.


Comments

There are no comments at the moment.