#P9731. Balanced Regrading Schedule

    ID: 22877 Type: Default 1000ms 256MiB

Balanced Regrading Schedule

Balanced Regrading Schedule

Due to a hacker attack on the judging machines, the contest committee has decided to regrade all submissions. There are \(N\) judging machines and \(T\) problems (numbered \(1, 2, \ldots, T\)). It has been predetermined which submissions each machine will judge; every machine has exactly \(S\) submissions (and \(S\) is a power of two). In the next \(S\) minutes, each machine will evaluate one submission per minute.

Each submission is for a certain problem. Owing to the fragility of the data storage machines, it is required that for every problem and for any two time instances, the difference in the cumulative number of submissions judged for that problem is at most \(1\). Equivalently, if you look at the schedule minute-by-minute, each problem can be judged at most once per minute (across all machines). This ensures that the evaluations for each problem are as evenly distributed in time as possible.

Your task is to construct a schedule by reordering, for each machine, the submissions assigned to it, so that in every minute the judged submissions (one per machine) are all for different problems.

inputFormat

The first line of input contains three integers \(N\), \(T\), and \(S\) where \(S\) is a power of two. Each of the next \(N\) lines contains \(S\) integers. The \(i\)-th line represents the list of problem IDs of the submissions on the \(i\)-th machine. It is guaranteed that the overall counts allow a schedule that satisfies the condition that in each minute, no problem appears more than once (i.e. every problem's submissions will be spread over different minutes).

outputFormat

Output \(N\) lines. The \(i\)-th line should contain \(S\) integers representing the order in which the \(i\)-th machine will judge its submissions. This ordering must satisfy that for every minute (i.e. when taking the \(j\)-th submission from each machine for \(1 \le j \le S\)), no problem ID appears more than once.

sample

2 2 2
1 2
1 2
1 2

2 1

</p>