Editorial for WC '15 Contest 3 S3 - Rescue Mission
Submitting an official solution before solving the problem yourself is a bannable offence.
For starters, we should precompute the 2-dimensional prefix sums of all rows and columns. This precomputation can be done in time, and will be helpful later for computing subsequent rescue zone sums in constant time. Let denote the sum of the rectangle consisting of rows to (inclusive), and columns to (inclusive). Then, we can compute it using the recursive formula:
where in the base cases of or . The entire prefix sum array can be computed while is being inputted, at no additional cost in time complexity.
Now, because all values of are non-negative, each rescue zone should extend as far up and left from its central cell as possible. We can call a rescue zone "complete" if it's able to extend all the way to the top-most row (row ) and to the left-most column (column ), without intersecting with the other rescue zone. We can let equal the sum of the complete rescue zone with central point . Computing is easy now that we have the prefix sums – just subtract the sum of the rectangle with rows to and columns to from the total sum of the rectangle from the corner to cell . As a result, we will end up with the sum of the backwards "L"-shaped slice of the grid. That is, .
Now, only the following cases are possible: either both rescue zones will be "complete", or one branch of one of the rescue zones is forced to stop short in the horizontal or vertical direction by the other.
...S.B .....S ..S..B
...S.B .....S ..S..B
SSSS.B SSSSSS ..S..B
.....B ....B. ..SBBB
BBBBBB BBBBB. SSS...
In the first case, the central point of one rescue zone is strictly above and to the left of the central point of the other. The best possible answer for this case will be the maximum value of across any pair of coordinates and that satisfy and . To actually compute this best answer, we can use dynamic programming as we iterate over all cells from the top-left corner of the grid to the bottom-right corner.
Let store the maximum complete rescue zone whose central point is in the inclusive rectangle from to . Intuitively, we can see that is equal to the maximum of three values: , , and . Finally, to get the max sum of any pair of complete, non-intersecting rescue zones, we can take the max of across all coordinates in the grid. This part of the solution will take time (and can in fact be fully computed as the grid is being inputted!).
The second case is that only one of the rescue zones is complete, while the other one has to stop short in either the horizontal or vertical direction so that it doesn't intersect with the first zone. Without loss of generality, let's say that the incomplete rescue zone's central point is to the right of the complete one's (see the rightmost figure above). It could instead be below it (as in the middle figure), but we can repeat the following algorithm with the grid's axes inverted to cover that case. Inverting the grid is as simple as treating row indices as column indices and vice versa.
Now, suppose the complete zone has its central point at and the incomplete zone has the central point (with and ) such that it includes cells to in row , and cells to in column . If we iterate over all possible cells from the bottom-right to the top-left while maintaining the sum of the best incomplete zone in each row (which can be updated in constant time as we go through each cell ), then the sum of the best accompanying incomplete zone for the complete zone centred at can also be looked up in constant time. As such, this part of the solution can also be implemented in time.
Taking the best answer of all of the cases will yield an solution.
Comments