Editorial for CCC '22 J5 - Square Pool


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.

The first subtask can be solved by recognizing that when there is only one tree, the largest square pool must sit at one of the "corners" of the tree. An example of this is drawn in the explanation for the first sample input where the largest pool can sit at the "bottom-left corner" of the tree. As a result, the first three marks can be earned by considering the four possible cases and taking the maximum value.

Once there is more than one tree, a lot more work must be performed. For the second subtask, an approach that uses brute-force will earn full marks. There are different ways to accomplish this but they all amount to essentially testing all possible locations and sizes of a square pool and for each possibility, determining whether or not it contains a tree. Doing this successfully is done most naturally using several nested loops.

Allowing yards to be as large as 500\,000-by-500\,000 prevents a brute-force approach from finishing within the time limit. However, an important observation for the third subtask is that the number of trees is still guaranteed to be quite small. This is a clue that we might want a solution that depends only on the number of trees and not on the size of the yard. Recursion can be used to do just this. The key idea of the first subtask can be reused here. By inspecting one tree, we can limit our further search for an optimal placement of the pool to above, below, to the left, or to the right of this tree. This reduces the problem to a smaller (now possibly rectangular) yard.

Recursion will fail once the number of trees climbs much higher than 10 because the runtime will grow exponentially as the number of trees increases. Therefore, we require a solution that still depends only on the number of trees, but to a lesser extent. A very clever idea allows us to accomplish this. A square pool that is as large as possible can be moved up until it hits a tree or the upper boundary of the yard. Similarly, a square pool that is as large as possible can be moved left until it hits a tree or the left boundary of the yard. If we imagine adding coordinates to the yard, this means that each tree and the upper boundary provide T + 1 candidates for the topmost position of an optimally placed pool. Similarly, there are T + 1 candidates for the leftmost position of an optimally placed pool. By considering all pairs consisting of a candidate for the leftmost position and a candidate for the topmost position, we considerably limit the number of possible locations for an optimally placed pool. Moreover, we can figure out the size of the largest possible pool at one of these possible locations by only considering the remaining trees and boundaries. This approach yields a solution with a runtime that computer scientists characterize as cubic in T.


Comments

There are no comments at the moment.