Editorial for IOI '08 P6 - Pyramid Base
Submitting an official solution before solving the problem yourself is a bannable offence.
We start off with a few observations regarding the placement of the optimal square:
Lemma 1
It suffices to consider squares whose left side touches either the left side of the field, or the right boundary of some obstacle.
Proof
We can always shift the optimal square leftwards until it hits such a boundary, without increasing the cost of placing it.
As and dimensions are independent in terms of translation, we also have:
Lemma 2
It suffices to consider squares whose bottom side touches either the bottom side of the field, or the upper boundary of some obstacle.
Proof
Similar to lemma 1, except we shift the optimal square downwards instead.
This observation alone yields an solution since we could now enumerate the possible locations of the bottom left corner of the square in time, then for each such location try increasing the square size and keep track of the obstacles we hit. It is not difficult to optimize this approach into an solution.
We now try to get a faster solution by turning the problem into a decision problem.
Suppose that we have a square we can afford. If we now shrink it, while keeping its lower left corner in place, we will get a smaller square we can afford. This means that if is the size of the optimal square, then for all there is a square of size we can afford, and there is no such square for any . As a result, we can binary search on the side length of the square and then solve the following problem:
Given a field with a set of rectangle obstacles, each with a given cost, find the square of side length such that the sum of costs of rectangles it intersects is minimum.
It is possible to solve this problem directly by sweeping and using some clever data structures. However, one simple observation can give us a solution that is pretty easy to implement. The observation is that all we actually need is the location of the bottom left corner of this square. Note that for each point we can easily compute which rectangles intersect a square of size with the bottom left corner in – these are precisely the same rectangles that contain the point , if we extend them by downwards and by to the left.
In other words, to solve our decision problem, we can extend each rectangle by both leftwards and downwards, and then solve a simpler problem:
Given a field with a set of rectangles, each with a given cost, find the point covered by rectangles of minimum total cost.
This problem can be solved by a range sweep going from left to right. As we encounter a rectangle's left value, we increment the range corresponding to it by its value, and when we pass a rectangle's right value, we decrement the range accordingly. So we need the following data structure:
Given an array of integers, support:
- Increasing/decreasing a section by a value.
- Query for the minimum value in the array.
Note that by lemma 2, it suffices to only consider values that are right above a top edge of a rectangle, so we have of these entries in the array. There are 3 basic data structures that support these requirements:
- A plain array, where each operation takes .
- A 2-level B-tree with each operation in .
- A range tree with each operation in .
More details on the construction of a range tree can be found in the solution for problem Fish. The solution with the range tree runs in , the last factor being the binary search for the square size. This solution was expected to receive 70 points, the one with the B-tree 55, and the one with the array 35 points.
The remaining 30 points were awarded for large cases with zero allowed cost. We will now outline one approach to solve these cases. This approach will originate from a different approach to the general problem.
Let be the largest cost we can afford. Define as the maximum vertical size of a rectangle with cost at most that has left edge on and right edge on . Then we can prove the following about the function :
Lemma 3
and also .
Proof
Take any rectangle with left edge at and right edge at . If we throw away the leftmost column, we get an equally tall rectangle with left edge at and right edge at . Obviously, the cost of the new rectangle is at most the cost of the old rectangle. Thus whenever we can afford rectangle , we can also afford rectangle (and we may even be able to make rectangle taller within our budget).
In terms of , the goal of the problem can be rephrased as finding and such that is maximized. Another way of stating the same goal is that we try to maximize over all pairs for which .
To find this maximal value, for each we can find the largest such that . We will use the notation for the largest such to the given .
Lemma 4
.
Proof
In words, if there is a valid square starting at and extending all the way to , there is also such a square starting at .
Formally, let . From the definition of , we have . From Lemma 3 it follows that . Combining these, we get . This means that is at least , which is exactly what we needed.
By Lemma 4 we know that we can calculate for all values by looping left to right on them, and incrementing the value for the current . This can be visualized as a double sliding window, as we insert rectangles as the right side of the window hits them and remove them as the left side leaves them. Clearly, each rectangle is inserted once and deleted once. The problem once again reduces to a data structure one:
We need a data structure that will maintain a set of weighted intervals and support:
- Insertion.
- Deletion.
- Finding the longest interval whose total cost is at most .
Note that this longest interval can be an arbitrary interval within the bounds of the field, and its cost is the sum of the costs of stored intervals it intersects.
In the general case where this structure is quite difficult to maintain and the host committee was not able to find a reasonable data structure that does it better than in time per operation.
When , the last requirement simplifies to finding the longest empty interval. This is maintainable using a range tree, by tracking, for each segment :
- The length of the empty segment that starts on the left end of .
- The length of the empty segment that ends on the right end of .
- The maximum length of an empty segment contained in .
These values then propagate nicely up the tree and each operation takes time. As in the previous solution, maintenance in time is also possible.
The cost for moving the left and right boundary during the sweep takes time if implemented naively. However, Lemma 1 along with the fact that the function can only change when the right boundary of the sweep reaches the left boundary of some rectangle means that we can move the boundaries in jumps, and process interesting events only.
The motivation for creating this problem came from the maximum axes-parallel empty rectangle problem, which in its simplest form can be phrased as follows: Given a set of point obstacles (having zero area, contrary to this problem) and a bounding rectangle, find the rectangle of maximum area that does not contain any of those points in its interior. Using the earlier lemmas, one could derive that all sides of the rectangle touch some of the points and obtain an solution. Sub-quadratic solutions are possible; one possibility is to use repeated divide-and-conquer followed by 3-dimensional half-space queries. None of the known solutions were suitable for the IOI, as already the implementations of 3D convex hulls are far from trivial. As a final note, Aggarwal and Suri gave an solution in 1989.
[1] A. Aggarwal, S. Suri, Fast algorithms for computing the largest empty rectangle, Proceedings of the third annual symposium on Computational geometry, p.278-290, June 08-10, 1987, Waterloo, Ontario, Canada.
Comments