#P4194. Matrix Balancing via Clamping

    ID: 17441 Type: Default 1000ms 256MiB

Matrix Balancing via Clamping

Matrix Balancing via Clamping

Given an integer matrix \(A\) of size \(n\times m\), you are to construct another matrix \(B\) of the same dimensions such that for every \(1\le i\le n\) and \(1\le j\le m\), the element \(B_{i,j}\) lies in the interval \([L,R]\). The goal is to minimize the following quantity:

[ \max\Biggl{ \max_{1\le j\le m} \left|\sum_{i=1}^n\bigl(A_{i,j}-B_{i,j}\bigr)\right|, \quad \max_{1\le i\le n} \left|\sum_{j=1}^m\bigl(A_{i,j}-B_{i,j}\bigr)\right| \Biggr} ]

A simple strategy that satisfies the constraints is to choose \(B_{i,j}=\min(\max(A_{i,j},L),R)\), i.e. to clamp \(A_{i,j}\) into the interval \([L,R]\). Although more refined methods might distribute the discrepancies to further reduce the maximum row or column error, this clamping approach guarantees that each \(B_{i,j}\) is as close as possible to \(A_{i,j}\) while respecting the bounds, and it often serves as a reasonable solution.

inputFormat

The first line of input contains four integers: \(n\), \(m\), \(L\), and \(R\), where \(n\) and \(m\) denote the dimensions of the matrix \(A\), and \(L\) and \(R\) are the lower and upper bounds for the elements of \(B\). Each of the following \(n\) lines contains \(m\) integers representing the matrix \(A\).

outputFormat

Output the matrix \(B\) in the same format: \(n\) lines where each line contains \(m\) space-separated integers. Each element \(B_{i,j}\) must satisfy \(L\le B_{i,j}\le R\).

sample

2 2 1 10
0 11
5 15
1 10

5 10

</p>