QOJ.ac

QOJ

Time Limit: 16 s Memory Limit: 1024 MB Total points: 100

#15516. Mountain Allocation

统计

The Danes are envious that all other countries have much better ski slopes. The issue has been brought all the way to the European Court of Justice, and it has been decided that the borders of European countries should be redrawn so that the distribution of ski slopes is as fair as possible. Europe can be modeled as a grid of size $R \times C$, where the cell at row $i$, column $j$ has a number $m_{ij}$ indicating how many mountains are in that cell. A country consists of a contiguous collection of cells, where the total number of mountains in the country divided by the area of the country determines how pleasant it is to ski there. A country is contiguous if it is possible to move within the country from each cell to every other cell, by moving only in the four cardinal directions (i.e., not diagonally). Help the Danes to divide Europe into $N$ countries so that the mountains are distributed as evenly as possible relative to the countries’ areas!

Input

The first line contains the integer $T$, the number of the testcase. The second line contains three integers $R$, $C$ ($2 \le R \times C \le 10^5$), and $N$ ($1 \le N \le 16000$), representing the number of rows and columns in the grid, and the number of countries, respectively. Then, there are $R$ lines, one for each row in the grid. Each row contains $C$ numbers. In row $i$, column $j$ the number $m_{ij}$ ($0 \leq m_{ij} \leq 1000$) represents the number of mountains at the corresponding location in the grid.

Output

The $N$ countries are numbered from $0$ to $N - 1$. Print $R$ rows with $C$ numbers each, where each number indicates the country number that owns the cell after your division.

Examples

Input

0
2 2 3
1 5
4 2

Output

0 0 
1 2

Explanation

problem_15516_1.png

Figure 1: Division of countries in Sample 1.

In the first sample, $\bar{a} = \frac{1 + 5 + 2 + 4}{4} = 3$. The average values are 3 for the red country, 2 for the blue country, and 4 for the green country. This gives $S = 2$.

Input

0
4 6 6
1 2 2 3 5 3
5 6 7 4 5 3
5 7 8 7 5 3
2 2 1 2 6 2

Output

0 0 0 1 1 1 
0 0 0 4 1 3 
0 5 5 3 3 3 
5 5 5 3 2 2

Explanation

problem_15516_2.png

Figure 2: Division of countries in Sample 2.

In the second sample, $\bar{a} = 4$. The average value in each country is 4. This gives $S = 0$, which is a perfect solution.

Points

Your solution will be tested on 10 test cases, each of which can earn up to 10 points. Let $a_k$ be the total number of mountains in country $k$ divided by the number of cells in the country, and let $\bar{a}$ be the total number of mountains divided by the total number of cells in the entire grid. You want to minimize the value of the expression

$$ S = \sum _{k=1}^{N}{(a_k - \bar{a})^2}. $$

If $S = 0$, your solution will earn 10 points for the test case. Otherwise, your score is determined by the expression

$$ 10 \times \max (1, \frac{S_D}{S}), $$ where $S_D$ is what the judge’s solution achieved on the same test case.

About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.