#K59477. Partitioning the Farm
Partitioning the Farm
Partitioning the Farm
Given a farm represented as an N × M grid where each cell contains a certain amount of crops, your task is to partition the farm into K contiguous horizontal subgrids. Each subgrid consists of consecutive rows of the original grid. The goal is to assign each cell a subgrid number (from 1 to K) by dividing the grid row‐wise such that the difference between the total amount of crops in the subgrid with the maximum crops and the subgrid with the minimum crops is minimized.
In this problem, a simple row‐wise partition is used. Let \( q = \lfloor \frac{N}{K} \rfloor \) and \( r = N \mod K \). Then, for the first \( r \) subgrids, assign \( q+1 \) rows each; for the remaining subgrids, assign \( q \) rows each. Although the crop values are provided, the partitioning is solely based on the number of rows.
Note: The input is read from stdin
and the output is written to stdout
.
inputFormat
The first line of input contains three integers N, M, and K (1 ≤ K ≤ N
), where N is the number of rows, M is the number of columns, and K is the number of subgrids.
The next N lines each contain M integers representing the crop amounts in each cell of the grid.
outputFormat
Output N lines, each containing M integers. The integer in the i-th row and j-th column represents the subgrid number assigned to that cell. Each subgrid number must be an integer between 1 and K according to the row-wise partitioning rule.
## sample4 4 1
1 3 2 6
5 9 8 4
4 7 1 2
3 5 6 9
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
</p>