Editorial for WC '16 Contest 4 S3 - Night Howlers
Submitting an official solution before solving the problem yourself is a bannable offence.
Let's consider binary searching for the answer – that is, the minimum valid howling radius . All values of smaller than the answer are invalid, while all values of larger than or equal to the answer are valid, meaning that the binary search will work out if we can just find a way to determine the validity of a given value of .
Given a fixed value of , we need to determine the minimum number of initial wolves which must be made to howl in order for all wolves to join in, and then compare this count to . Rather than attempting to deal with chain reactions of wolves causing each other to howl, an insight can be made to simplify the problem greatly: Each wolf must be made to howl if and only if there exists no wolf which would directly cause wolf to howl (in other words, there exists no such that , , and ). If there's indeed no such wolf , then no matter what set of other wolves are made to howl, wolf will surely never start howling until Judy and Nick make him howl themselves. If there is such a wolf , then Judy and Nick will need to ensure that wolf will end up howling one way or another, at which point wolf will also join in.
What remains is determining whether or not any valid wolf exists for each wolf . The simplest way to do so is to sort the wolves in increasing order of rank (which can be done once in time, before the start of the binary search), and then iterate over them in this order while maintaining an ordered set of the positions of wolves considered so far. For each wolf , we only need to check whether any position in the current set is in the interval (as any such wolf would be guaranteed to also have a smaller rank), before proceeding to insert itself into the set. If the set is implemented as a balanced binary search tree, this query and update operations can each be performed in time. As such, the overall time complexity of this algorithm is , where is the maximum of the values (which dictates the number of iterations required for the binary search).
Comments