Editorial for DMOPC '21 Contest 6 P3 - An Art Problem
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1
For subtask 1, we can simulate the expansion process as described in the problem. For each of the blank pixels, we can loop through all coloured pixels to find the closest pixel, breaking ties by the colour number. This takes time overall.
Time Complexity:
Subtask 2
Subtask 2 was primarily created to reward subtask 3 solutions that do not correctly break ties by the colour number.
Alternatively, we can note that each coloured pixel "covers" a tilted square region, and it suffices to simply find whether each pixel is covered or not when all the colours are the same. We can do this in time using a difference array for each row. For each coloured pixel, loop through the rows closest to it and add to the subinterval of this row that is covered by the expanded region of this pixel. A pixel is coloured in the final drawing if the number at this pixel is greater than after all the updates.
Time Complexity:
Subtask 3
For subtask 3, we can simulate the expansion process in iterations. In each iteration, consider all currently blank pixels and check if one of its neighbouring pixels is coloured. If so, we colour this pixel with the colour of the smallest-numbered neighbour. This takes time overall.
Time Complexity:
Subtask 4
Subtask 4 was primarily created to reward full solutions that do not correctly break ties by the colour number.
Alternatively, we can rotate the grid by and use 2D difference arrays to update the covered area of each coloured pixel, similar to subtask 2.
Time Complexity:
Subtask 5
For subtask 5, we can perform a simultaneous BFS from the coloured pixels. During the BFS, each node stores the distance to the nearest coloured pixel as well as the colour of the smallest-numbered nearest pixel. In order to break ties by the colour number properly, whenever we find a different path to a node with the same distance but smaller-numbered source pixel, we update the nearest colour stored for this node. Note that it is not necessary to push this node into the queue again, as this node will always still be in the queue when this happens.
Other approaches are possible, including:
- Sorting the coloured pixels by their number initially and pushing them into the BFS queue in that order. This ensures that the first path we consider to a node has the smallest-numbered source pixel, so a normal BFS works from here.
- Performing a grid traversal that more closely resembles Dijkstra's algorithm, using a priority queue to break ties by the colour of the source pixel.
Time Complexity: or
Comments