Editorial for COCI '09 Contest 3 #4 Razgovori


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.

First, note that a greedy strategy finds the optimal solution. Each time a detector detects calls on location i, we will presume that the call is made between the leftmost and rightmost house possible. This means that all detectors between those two houses must detect at least one more call. Naive implementation of this solution can lead to \mathcal O(N \log N + NC) time complexity and 50\% of points.

Let's try to form a better solution. We start by sorting the detectors by position. We now start from the left most detector, and maintain a stack of current calls. Of course, at the first detector, we add C_1 calls to the stack. We now process detectors one by one in order. If the number of calls detected by the current detector is greater than the number of calls on the stack, we add more calls to the stack. If it is smaller, we reduce the number of stack. We can now solve the problem by counting the number of times we remove items from the stack.


Comments

There are no comments at the moment.