Editorial for Google Code Jam '22 Qualification Round Problem C - d1000000
Submitting an official solution before solving the problem yourself is a bannable offence.
Test Set 1
There are multiple ways to solve Test Set 1 of this problem. A particularly funny one is to throw the solution of an old finals problem at it (even the Test Set 1 solution of that problem works).
Test Set 2
Test Set 2 has very big numbers, so we need insights that are specific to this problem.
Insight 1. If a straight from to can be done, then one from to can be done as well using the same dice in the same order, since a die showing a number can always be used to show number .
Insight 2. If a straight is done with a showing number and a showing number with , we can build the same straight but using for and for .
Insight 2b. Any straight that can be done, can also be done while using the dice in non-decreasing order of number of faces.
Combining insights 1 and 2b gives an algorithm: start by sorting the dice. Then, in that order, try to extend the current straight if possible. Or, in pseudo-code:
maximum_straight_length(S):
sort(S)
length = 0
for si in S:
if si > length: length += 1
return length
This algorithm requires only linear time beyond sorting the input, which means overall. This is fast enough to pass Test Set 2.
Comments