Editorial for COI '13 #2 Histogrami
Submitting an official solution before solving the problem yourself is a bannable offence.
We solve this task with dynamic programming. Let mark the minimal mistake of constructing a histogram with given points such that the rightmost point of the histogram is on the -coordinate . Now we could conceptually solve this problem by trying to connect each point on the coordinate with the points left to it on the same height (-coordinate), which would lead us to the correct solution of complexity , which is too slow.
This complexity implicitly assumes that the problem of determining the function or between two points is solved in complexity of .
In order to speed up the given algorithm, let's notice that when calculating it is enough to observe only the previous coordinate of a certain point (because the histogram can arbitrarily change the height without any additional errors).
This observation, along with a careful implementation, leads us to complexity which was enough for of points.
To further speed up the algorithm, it is necessary to calculate and between two points in complexity instead of . Let's show that this is possible for (the more difficult case), whereas we'll leave for you to practice.
Let's notice that between two points on the same coordinates and is equal to , where marks a point (sometimes fictional) on -coordinate with equal height as and . In other words, it is necessary for each point to determine only .
This problem can be solved for all points in complexity by using Fenwick tree (logarithmic structure) or segment (tournament) tree and scanline algorithm in the direction of the increasing -coordinate.
Complexity:
Comments