#P7239. Minimal Blocks with Single Peak Constraint

    ID: 20440 Type: Default 1000ms 256MiB

Minimal Blocks with Single Peak Constraint

Minimal Blocks with Single Peak Constraint

You are given a base that can be viewed as an \(n \times m\) grid. In each cell you may stack an arbitrary number of blocks. Due to gravity the blocks will never float in the air, and there is no glue to stick them together.

From the 3D block structure, three orthogonal projections (views) are provided:

  • The side view: an array of \(n\) non‐negative integers representing the maximum block height in each row.
  • The front view: an array of \(m\) non‐negative integers representing the maximum block height in each column.
  • The top view: can be deduced – a cell has a block if and only if it contributes to one of the two views above.

Your task is to construct an \(n \times m\) matrix \(h\) where each \(h_{i,j}\) is a nonnegative integer, such that:

  1. For every row \(i\) the maximum value equals the given side view value, and for every column \(j\) the maximum equals the given front view value.
  2. For each row \(i\), if we view the row as a sequence \(h_{i,1}, h_{i,2}, \dots, h_{i,m}\) and consider the bordering cells as having value \(0\), then there is at most one peak. Similarly, each column (when viewed top‐to‐bottom and padded with 0) has at most one peak. A peak in a sequence \(a_1,a_2,\dots,a_k\) (with \(a_0=a_{k+1}=0\)) is an index \(i\) such that \[ a_i > a_{i-1} \quad\text{and}\quad a_i > a_{i+1}. \] For example:
Arrangement Number of Peaks
123 (i.e. 1 2 3) 1
212 2
122221 1
00011000 1
10010101 4

You are to use the minimum total number of blocks while satisfying the views and the peak constraints. If there is no valid configuration, output -1.

Observation and Construction: In order to minimize the total number of blocks, it is optimal to put blocks only where necessary. Notice that if a row (with side view value > 0) has only one nonzero cell (placed so that its value equals the given maximum) and similarly each column with a positive front view value gets exactly one nonzero cell equal to that value, then the peak condition is satisfied (since the maximum in that row/column will be a peak and all other entries are 0, which do not form a peak).

This leads to the following necessary condition: For every positive integer \(v\), the number of rows whose side view value is \(v\) must equal the number of columns whose front view value is \(v\). Then, assign each row with value \(v\) a unique column with value \(v\) and set that cell to \(v\), with all other cells set to 0. The total number of blocks is then \(\sum_{i=1}^{n} h_{i,j_i}\), which is minimum.

If the necessary matching condition fails, then no valid configuration exists; output -1 in that case.

inputFormat

The input consists of three lines:

  1. The first line contains two integers \(n\) and \(m\) \((1 \leq n, m \leq 1000)\), representing the number of rows and columns respectively.
  2. The second line contains \(n\) nonnegative integers, representing the side view: the maximum block height for each row.
  3. The third line contains \(m\) nonnegative integers, representing the front view: the maximum block height for each column.

Note: A value of 0 means that the corresponding row/column should have no blocks (i.e. no peak).

outputFormat

If there exists a valid configuration, output the minimum total number of blocks on the first line, followed by \(n\) lines each containing \(m\) integers (separated by spaces) representing the block heights in each cell of the matrix. If there is no solution, output -1.

sample

3 3
0 5 0
0 5 0
5

0 0 0 0 5 0 0 0 0

</p>