Editorial for WC '15 Contest 4 S4 - Stakeout
Submitting an official solution before solving the problem yourself is a bannable offence.
For starters, we need to determine which buildings each agent can see. After sorting the positions of the buildings in time, we can determine the interval of buildings that each agent can cover in time using binary search, by searching for the first building whose position is no smaller than , and the last building whose position is no greater than .
As for answering the questions, we can observe that , meaning that for any , hiring any subset of agents with indices smaller than is worth it if it means we can avoid hiring agent . This suggests the approach of iterating over the agents downwards from to , and for each one, only hiring them if necessary – in other words, if skipping that agent and hiring all of the agents with indices smaller than wouldn't yield sufficient coverage of the buildings.
To implement this approach, we can start by assuming that we'll hire all agents, and then for each agent , we can try removing them and testing if each building is still covered by enough agents – if not, agent must be necessary, so we must re-insert them into the set of hired agents and add onto the total necessary cost (assuming that we've precomputed the values of for ). If even hiring all agents to start with isn't sufficient, then the answer can immediately be determined to be -1
.
What remains is being able to execute the above operations efficiently. In particular, we must be able to insert and remove agents from the set of hired agents, and test if all buildings are in sight of at least hired agents. If we imagine an array such that is the number of hired agents that building is in sight of, then these operations equate to adding to an interval of values, subtracting from an interval of values, and determining the minimum value in the array.
Now, each of these operations can be handled by a segment tree with lazy propagation in time. Each node in the tree should store the minimum value in its interval, as well as a lazy value of how much should be added to (or subtracted from) its entire interval. Conveniently, adding a constant value to every index in an interval results in that interval's minimum value also increasing by .
For each question, we hire all of the agents and then iterate over all of them in an attempt to remove them, performing one or two segment tree operations each time. Therefore, the total time complexity is .
Comments