Editorial for WC '15 Contest 3 S3 - Rescue Mission


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.

For starters, we should precompute the 2-dimensional prefix sums of all rows and columns. This precomputation can be done in \mathcal O(NM) time, and will be helpful later for computing subsequent rescue zone sums in constant time. Let sum[i][j] denote the sum of the rectangle consisting of rows 1 to i (inclusive), and columns 1 to j (inclusive). Then, we can compute it using the recursive formula:

\displaystyle sum[i][j] = P[i][j]+sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]

where sum[i][j] = 0 in the base cases of i = 0 or j = 0. The entire prefix sum array can be computed while P is being inputted, at no additional cost in time complexity.

Now, because all values of P 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 1) and to the left-most column (column 1), without intersecting with the other rescue zone. We can let C[i][j] equal the sum of the complete rescue zone with central point (i, j). Computing C[i][j] is easy now that we have the prefix sums – just subtract the sum of the rectangle with rows 1 to i-1 and columns 1 to j-1 from the total sum of the rectangle from the corner (1, 1) to cell (i, j). As a result, we will end up with the sum of the backwards "L"-shaped slice of the grid. That is, C[i][j] = sum[i][j]-sum[i-1][j-1].

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 C[i_1][j_1]+C[i_2][j_2] across any pair of coordinates (i_1, j_1) and (i_2, j_2) that satisfy 1 \le i_1 < i_2 \le N and 1 \le j_1 < j_2 \le M. To actually compute this best answer, we can use dynamic programming as we iterate over all cells (i, j) from the top-left corner of the grid to the bottom-right corner.

Let DP[i][j] store the maximum complete rescue zone whose central point is in the inclusive rectangle from (1, 1) to (i, j). Intuitively, we can see that DP[i][j] is equal to the maximum of three values: DP[i-1][j], DP[i][j-1], and C[i][j]. Finally, to get the max sum of any pair of complete, non-intersecting rescue zones, we can take the max of C[i][j]+DP[i-1][j-1] across all coordinates (i, j) in the grid. This part of the solution will take \mathcal O(NM) 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 (i, j) and the incomplete zone has the central point (r, c) (with r \le i and c > j) such that it includes cells j+1 to c in row r, and cells 1 to r in column c. If we iterate over all possible (i, j) cells from the bottom-right to the top-left while maintaining the sum of the best incomplete zone in each row r (which can be updated in constant time as we go through each cell (i, j)), then the sum of the best accompanying incomplete zone for the complete zone centred at (i, j) can also be looked up in constant time. As such, this part of the solution can also be implemented in \mathcal O(NM) time.

Taking the best answer of all of the cases will yield an \mathcal O(NM) solution.


Comments

There are no comments at the moment.