Editorial for WC '15 Contest 3 J2 - Electroshock Therapy
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 (for ) is equal to (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 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