Editorial for WC '15 Contest 3 J4 - LexCorp Infiltration
Submitting an official solution before solving the problem yourself is a bannable offence.
If not for type-2 events requiring the grid to be constantly changed, this would be a relative standard flood-fill problem. As a first step, we can locate all of the control rooms, assigning them an index number from to some integer (note that ), determining their sizes, and remembering which room each empty cell belongs to. This can be accomplished in time using either depth first search (recursion) or breadth first search. We can iterate through the cells of the grid in any order, and when we come across an empty cell which hasn't been assigned to a room yet, we should perform a search to locate its entire room by recursively expanding to adjacent, unvisited empty cells on its top, right, bottom, and left sides (if they exist). An easy trick to remove the need for range checks is to initially pad an entire -based grid with wall tiles before inputting the actual grid into -based indices. This effectively creates a "border" around the grid to prevent the search from going out of bounds.
For each round of filling, we can just choose the index as the current number of rooms that have been found so far, starting with for the first room. We will also keep track of the area for each room (incrementing it whenever we reach a cell). At the end of filling each room, we record the index of the room associated with its area.
Next, we should determine the initial ranks of the rooms. To do this efficiently, we must first sort the rooms in non-increasing order of size, which can be done in time. To do this in most languages, we can encapsulate the room data into pairs of and define a comparator for the sorting function to compare by the sizes rather than the room indices. Finally, their ranks can be computed in time with a single sweep through them, ensuring that each room gets the same rank as the previous room if they're of equal area, or a rank of otherwise. When there are only type-1 events, nothing more is required – each event can be processed by looking up the index of the room that cell is part of, and then looking up that room's rank.
However, type-2 events make things trickier, as they can cause various rooms' ranks to change. That being said, there's no need to re-compute the ranks of all rooms – it can be observed that, when a room is eliminated, the only effect is that the ranks of all rooms with strictly smaller sizes decrease by . Since there are such rooms each time, processing all events has a time complexity of . Since and ( is as large as ), that yields roughly billion steps. So, even with this observation, looping over all such rooms and updating their ranks is too slow for full marks.
However, this logic can be rearranged as follows: a room's rank is equal to its initial rank, minus the number of strictly larger rooms which have been eliminated so far. Instead of keeping the ranks of every room up to date, we can instead handle each event by looping over all previous type-2 events and counting the number of larger rooms which were targeted in them (taking care to not double-count rooms which were targeted multiple times). This yields a time complexity of . Plugging in the upper bounds for , and , we are in the ballpark of , or roughly million steps, which is acceptable.
Finally, it's worth noting that this problem can be solved even more efficiently, in time, by using an advanced data structure such as a binary indexed tree to speed up the step of counting previously-eliminated larger rooms from to running time.
Comments