Editorial for WC '15 Contest 3 J2 - Electroshock Therapy


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.

Upon studying the diagram given in the description, we see that the wire segments can be counted in several groups. If for each building we are able to compute the length of horizontal segments on its center and top, as well as vertical segments on only its left side, then we can add it all up for each building. After that, we must account for the right side of the last building, which may be added in afterwards to get the answer.

Inspecting the diagram, we see that the number of horizontal segments for each building i (for i = 1 \dots N) is equal to H_i+1 (one for each floor, plus the roof). We also see that the number of vertical segments between any two adjacent buildings is equal to the maximum of their heights. So, for the left of the first building and the right of the last building, we can simply take their heights without taking the max. As such, we can loop through the buildings in \mathcal O(N) time and sum up these values to get the total length of wire.

One last trick is noticing that we don't even need to store the heights in an array. We just have to always keep track of the height of the previous building and use it to take the max the next time.


Comments

There are no comments at the moment.