Editorial for WC '16 Contest 4 S3 - Night Howlers


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
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 R. All values of R smaller than the answer are invalid, while all values of R 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 R.

Given a fixed value of R, we need to determine the minimum number of initial wolves which must be made to howl in order for all N wolves to join in, and then compare this count to K. 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 i must be made to howl if and only if there exists no wolf j which would directly cause wolf i to howl (in other words, there exists no j such that A_j < A_i, P_j \ge P_i-R, and P_j \le P_i+R). If there's indeed no such wolf j, then no matter what set of other wolves are made to howl, wolf i will surely never start howling until Judy and Nick make him howl themselves. If there is such a wolf j, then Judy and Nick will need to ensure that wolf j will end up howling one way or another, at which point wolf i will also join in.

What remains is determining whether or not any valid wolf j exists for each wolf i. The simplest way to do so is to sort the wolves in increasing order of rank (which can be done once in \mathcal O(N \log N) 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 i, we only need to check whether any position in the current set is in the interval [P_i-R, P_i+R] (as any such wolf would be guaranteed to also have a smaller rank), before proceeding to insert P_i 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 \mathcal O(\log N) time. As such, the overall time complexity of this algorithm is \mathcal O(N \log N \log M), where M is the maximum of the P values (which dictates the number of iterations required for the binary search).


Comments

There are no comments at the moment.