Editorial for IOI '05 P1 - Garden
Submitting an official solution before solving the problem yourself is a bannable offence.
Let us call a rectangular region containing exactly roses a -rectangle. Let us also denote by the number of roses in the square . The problem is to find two disjoint -rectangles with minimal sum of perimeters.
The simplest solution is to consider all possible rectangular regions of the garden, and for each of them to count the number of roses inside. This way we can enumerate all -rectangles in time. There may be up to -rectangles in total. The second step is to consider all the pairs of -rectangles and choose the one consisting of disjoint regions with minimum sum of perimeters. Such a solution should receive about points, despite its terrible time complexity of .
Model solution
Now we will present a number of gradual improvements, which will finally yield us the model solution. Please note, that we can optimize checking if a given rectangular region is a -rectangle. Let us denote by the number of roses in a region, with one corner at and the opposite one at . We can precompute all the values of iteratively in time using the following formula:
Having this, we can express — the number of roses in a rectangular region with corners and as:
This way, can be evaluated in time. Using the presented method, we can enumerate all -rectangles in time. Unfortunately, this does not solve the problem of considering all pairs of -rectangles.
But fortunately, there is another method, which copes with this problem. Please observe, that if we have two disjoint rectangular regions, then there must exist either a horizontal or a vertical line such that one rectangle is above it (or respectively to the left) and the other one is below it (or respectively to the right). Hence, for each horizontal line we can find two -rectangles with the smallest perimeters, laying on the opposite sides of the line. Similar values are to be found for all vertical lines. When we have done this, we can easily compute the final result in by considering all the possible dividing lines and choosing the result which gives us the optimal sum of perimeters.
Now we will show how to find optimal perimeters for the first case (rectangular regions above the given horizontal line). The three other cases can be solved analogously. Let us denote by the minimal perimeter of -rectangle laying above the horizontal line with the given -coordinate, whose bottommost coordinate is greater than or equal . Let us also denote by the minimal perimeter of the -rectangle with bottommost coordinate equal . Please note, that:
A simple way to calculate 's is to initially set them to infinity, and then update them while iterating through all -rectangles. With this improvement, our algorithm works in time.
This is not all. Please note, that we do not need to consider all -rectangles. We can limit our considerations to those -rectangles, which do not contain any other -rectangles in their interiors. To enumerate all interesting -rectangles (and maybe some not interesting too), we consider all pairs , . For each such pair, we use a sliding window, which is a rectangle having corners at and . At the beginning, . Then we repeatedly move the sliding window according to the following rules, until :
- if there are exactly roses in the sliding window (i.e. ), then we have found a new -rectangle; after updating the four constructed sequences ( and the three other analogous sequences), is incremented by one,
- if then is incremented by one, to expand the sliding window,
- if then is incremented by one, to shrink the sliding window,
The above algorithm works in time, and enumerates (among others) all interesting -rectangles. Of course, we can reduce its running time to by adapting the direction, in which this method works.
The presented solution, with all the above optimizations, constitutes the model solution.
Comments