Editorial for WC '15 Contest 4 S3 - Coded Paper


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.

We are given a 2 by N matrix of numbers and want to maximize their sum by replacing any number of rectangular regions on the matrix with other rectangles, each of which has just the number R written on it. For the first subtask where N \le 2, we can manually generate every possible configuration of rectangle placements. For the second subtask where N \le 6, we can do some kind of brute force to find the answer (possibly using backtracking to repeatedly place rectangles of all possible sizes in all possible configurations).

For an answer that gets full marks, let's first consider the simple case in which R is non-negative. In this case, there's no benefit to using larger cardboard rectangles – they might as well all be 1 by 1 squares to maximize the number of numbers we can replace. In particular, we should just greedily cover every cell with value C_{i,j} less than R.

When R is instead negative, dynamic programming is required. Let DP[i][s] be the maximum possible sum of values in the first i columns, where s is an integer between 0 and 4 describing the state of any ongoing cardboard rectangles. In particular, the following possibilities exist at any column i:

  • 0 – No ongoing rectangles
  • 1 – Ongoing height-1 rectangle in row 1
  • 2 – Ongoing height-1 rectangle in row 2
  • 3 – Ongoing height-1 rectangles in both rows
  • 4 – Ongoing height-2 rectangle spanning both rows

DP[i][s] is computed by considering each possible previous rectangle state s' (between 0 and 4), and maximizing the value of: DP[i-1][s'] + \{sum of visible cells in column i given the rectangles described by s\} + R \times \{number of new rectangles being started going from state s' to s\}.

This requires some case analysis of the 5 possible rectangle states and the transitions between pairs of them.

The final answer is stored at case 0 of the last column, where there are no ongoing rectangles. The time complexity of this algorithm is \mathcal O(N).


Comments

There are no comments at the moment.