Editorial for TLE '17 Contest 3 P5 - Hypercube Hotel
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
There are two main ways of interpreting the problem statement.
The most literal interpretation from the problem text is the one where the rooms are numbered according to a mixed-radix number system.
The other important interpretation is that the rooms are cells in a hyper-rectangular grid (as roughly implied by the name of the hotel) with dimensions
First, the property of a room not being a neighbour of itself is a bit of misdirection. We can pretend that a room really is a neighbour of itself, then subtract a stored value from each cell just before we output our answer.
Next, note that it is too costly to sum up the entire
Indeed, the solution has a dynamic programming flavour to it.
Formally, define
(this recurrence ignores edge cases) where
This algorithm is more intuitive when considered geometrically.
We make
Finally, the time limit set for this problem is strict, to prevent less sophisticated algorithms from passing.
Thus, as much as it is nice to understand the algorithm conceptually, it is also required to design a fast implementation.
To begin, it is ideal to store the values in a one-dimensional array, and index entries using the mixed-radix number system discussed.
Of course, nine-dimensional arrays are an option if you are crazy brave enough.
To help with constant optimization, keep in mind that compilers are your friends - structure your code in a way that encourages automatic vectorization.
This often means keeping the bodies of the innermost loops - typically the main bottlenecks of program execution - solely consisting of basic arithmetic operations, and not if statements.
Another relevant principle is locality of reference.
The time complexity is
Exercise: Generalize the algorithm so that corresponding digits of neighbouring rooms can differ by at most
Comments