QOJ.ac

QOJ

Time Limit: 3.0 s Memory Limit: 256 MB Total points: 100

#17674. NM 字符

Statistics

Busy Beaver 正在为他的语言学课程创造一门新语言!他已经决定这门语言的字母表由整数 $1, \dots, NM$ 按顺序组成;现在他想为这门语言造一些单词。

由于 Busy Beaver 对统计学很感兴趣,他想要控制单词中字母出现的频率,因此他选择了一个大小为 $NM$ 的字母多重集 $a$。他现在要组成 $N$ 个单词,每个单词包含 $M$ 个字母,使得多重集 $a$ 中的每个字母恰好被使用一次(即,如果某个字母 $x$ 在 $a$ 中出现了 $k$ 次,那么它在所有 $N$ 个单词中也恰好出现 $k$ 次)。

在组成这些单词后,Busy Beaver 计划将它们整理成一本字典,因此他会将这 $N$ 个单词按字典序排序,得到单词序列 $s_1, \dots, s_N$。Busy Beaver 喜欢字典序较小的字符串,因此对于每个从 $1$ 到 $N$ 的 $k$,他想知道在所有可能的单词组合方式下,$s_k$ 的字典序最小值是多少。

注意,每个 $s_k$ 的答案是独立的;例如,使得 $s_1$ 最小化的单词选择方式可能与使得 $s_2$ 最小化的方式不同。

输入格式

第一行包含两个正整数 $N, M$ ($1 \le NM \le 10^6$)。

第二行包含 $NM$ 个整数 $a_1, \dots, a_{NM}$,构成了多重集 $a$ ($1 \le a_j \le NM$)。

输出格式

对于每个从 $1$ 到 $N$ 的 $k$,在单独的一行中输出 $s_k$ 的字典序最小值,以空格分隔的整数序列形式表示。

样例

样例输入 1

4 3
1 1 2 2 3 3 4 4 5 5 6 6

样例输出 1

1 1 2
1 2 3
2 2 3
2 3 4

样例输入 2

3 4
12 4 4 4 1 1 4 4 6 4 1 4

样例输出 2

1 1 1 4
1 4 4 4
1 4 4 12

样例输入 3

4 5
12 7 17 3 6 11 9 2 17 7 1 6 9 12 6 3 9 12 2 15

样例输出 3

1 2 2 3 3
2 2 3 3 6
2 3 6 7 7
3 3 6 6 6

样例输入 4

1 1
1

样例输出 4

1

说明

在第一个样例中,以下单词选择方式使得每个 $s_k$ 最小化:

通过选择单词 $112, 233, 445, 566$,我们得到 $s = 112, 233, 445, 566$,因此 $s_1 = 112$(符合要求)。

通过选择单词 $123, 456, 123, 456$,我们得到 $s = 123, 123, 456, 456$,因此 $s_2 = 123$(符合要求)。

通过选择单词 $166, 155, 344, 223$,我们得到 $s = 155, 166, 223, 344$,因此 $s_3 = 223$(符合要求)。

通过选择单词 $234, 234, 156, 156$,我们得到 $s = 156, 156, 234, 234$,因此 $s_4 = 234$(符合要求)。

可以证明,在所有这些情况下,没有办法选择单词使得相应的 $s_k$ 在字典序上更小。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.