题目描述
Problem Description
Takahashi is planning to host a contest.
The contest is divided into $ n $ preliminary rounds and one final round, with the top $ k $ contestants from each preliminary round being invited to the final round.
The finalists are selected as follows:
- Contestants are selected in order of their highest rank across all preliminary rounds.
- If two contestants have the same highest rank, the one whose highest rank was achieved in an earlier preliminary round is selected first.
- This process continues until $ k $ contestants are selected.
Takahashi wants to send invitations to the finalists as soon as possible. Therefore, he wants to send emails to the contestants who have secured their spots in the final round after each preliminary round.
Given the results of each preliminary round, determine who should be emailed after each round. Note that no contestants will decline the invitation to the final round.
Input Format
The input is given from the standard input in the following format:
$ n $ $ k $ $ a_{1,1} $ $ a_{1,2} $ ... $ a_{1,k} $ $ a_{2,1} $ $ a_{2,2} $ ... $ a_{2,k} $ : $ a_{n,1} $ $ a_{n,2} $ ... $ a_{n,k} $
- The first line contains the number of preliminary rounds $n\ (1\ ≦\ n\ ≦\ 50)$ and the number of finalists $k\ (1\ ≦\ k\ ≦\ 1000)$, separated by a space.
- The next $ n $ lines contain the IDs of the top $ k $ contestants from each preliminary round. The $i\ (1\ ≦\ i\ ≦\ n)$th line represents the results of the $ i $th preliminary round. The results are given as $ k $ space-separated integers, where the $ j $th integer $a_{i,j}\ (1\ ≦\ a_{i,j}\ ≦\ 999999)$ is the ID of the contestant who ranked $ j $th in the $ i $th preliminary round. It is guaranteed that $a_{i,p}\ ≠\ a_{i,q}$ when $p\ ≠\ q$.
Output Format
For each preliminary round $i\ (1\ ≦\ i\ ≦\ n)$, output the IDs of the contestants who have secured their spots in the final round on the $ i $th line, separated by spaces. If there are multiple contestants, output them in ascending order of their IDs. Include a newline at the end of the output.
Sample Input and Output
Sample Input #1
2 11
1 2 3 4 5 6 7 8 9 10 11
1 2 15 14 13 16 17 18 19 20 21
Sample Output #1
1 2 3 4 5 6
7 13 14 15 16
Sample Input #2
4 5
1 2 3 4 5
2 1 3 4 5
1 2 3 4 5
2 1 3 4 5
Sample Output #2
1 2
3
4 5
Notes/Hints
Sample Explanation 1
There are 2 preliminary rounds. After the first round, the top 6 contestants are guaranteed a spot in the final round regardless of the results of the second round. During the second round, contestants with IDs 1 and 2 are duplicated, so the spots in the final round are confirmed for the top 7 from the first round and the top 6 from the second round. Note that the output order is in ascending order of IDs, not ranks.
Sample Explanation 2
Note that there may be preliminary rounds where no contestants secure their spots in the final round.
输入格式
见题面 Input 部分
输出格式
见题面 Output 部分