Editorial for COCI '20 Contest 5 #2 Po
Submitting an official solution before solving the problem yourself is a bannable offence.
The problem is equivalent to starting from the given sequence and reaching all zeros in the minimum number of decreasing steps.
The main insight is that if we take a number
It is also clear that the minimal nonzero element of the sequence should be taken to zero in a single decrease. This gives us a solution for
We can make this approach fast using a segment tree, but the intended solution is simpler.
Traverse the sequence from left to right. It can be proven that each element contributes
We can efficiently find whether each element satisfies the above condition using a monotone stack. We push values to the top of the stack after popping the stack until the new element is not smaller than the old top of the stack. If the top and the new element have equal values, we don't add
Comments