Editorial for DMOPC '22 Contest 1 P2 - Hat Swap
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Notice this problem can be visualized by viewing the action of "swapping" as travelling along an edge on a graph with node
Subtask 1
For the first
We can simulate this process by first letting Shirogane swap to the closest person with color
For each case, we can run breadth-first search once for each person to find the closest hat still available to that person, resulting in a time complexity of
Subtask 2
To get full score, we have to optimize the process of searching for the best answer for each hat color. Notice that there are two main cases of an optimal answer for some hat color
- The closest hat of color
from node (Shirogane) and (Kaguya) are different. In this case they both take the closest hat to them. - The closest hat of color
from both of them are the same. In this case we have to let a person take their second closest choice.
So we have to keep track of the closest and second closest node of each hat color for both Shirogane and Kaguya, which can be done by running breadth-first search once from node
Comments