QOJ.ac

QOJ

Time Limit: 1.0 s Memory Limit: 512 MB Total points: 100

#17626. 积木

Statistics

你有 $n$ 个木块,它们有 $k$ 种不同的颜色,排成一行。这些木块的颜色为 $c_1, c_2, \dots, c_n$,颜色编号均在 $1$ 到 $k$ 之间。

如果某种颜色的所有木块的位置平均值为 $(n + 1)/2$,则称该颜色是平衡的。注意,这个数值不一定是一个整数。

problem_17626_1.png

例如,如果 $7$ 个木块按上图方式排列,颜色 $1$ 的平均位置为 $(1 + 4 + 6)/3 = 11/3$,颜色 $2$ 的平均位置为 $(2 + 3 + 7)/3 = 4$,颜色 $3$ 的平均位置为 $5$。由于 $(7 + 1)/2 = 4$,颜色 $2$ 是平衡的,但颜色 $1$ 和 $3$ 不是。

你能否将这些木块重新排列,使得所有颜色都是平衡的?

输入格式

第一行包含一个整数 $t$:测试用例的数量。

接下来描述各个测试用例,每个测试用例包含两行:

第一行包含两个整数 $n$ 和 $k$:木块的数量和不同颜色的数量。

第二行包含木块的颜色 $c_1, c_2, \dots, c_n$。每种颜色至少出现一次。

输出格式

对于每个测试用例,如果存在解,输出 "YES",否则输出 "NO"。如果存在解,在下一行输出 $n$ 个整数,描述一种可能的排列顺序:即每个位置上的木块颜色。

数据范围

  • $1 \le t \le 100$
  • $1 \le k \le n \le 2 \cdot 10^5$
  • $1 \le c_1, c_2, \dots, c_n \le k$
  • 所有 $n$ 的总和不超过 $2 \cdot 10^5$

样例

输入 1

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

输出 1

YES
1 2 2 1 1 1 2
NO
YES
1 1

说明 1

在第一个测试用例的输出中,颜色 $1$ 是平衡的,因为其平均位置为 $(1 + 4 + 5 + 6)/4 = 4$,等于 $(n + 1)/2$。同理,颜色 $2$ 的平均位置为 $(2 + 3 + 7)/3 = 4$。因此两种颜色都是平衡的。

在第二个测试用例中,两种可能的排列方式都无法使颜色平衡。

在第三个测试用例的输出中,颜色 $1$ 的木块出现在位置 $1$ 和 $2$。平均值为 $3/2$,因此颜色 $1$ 是平衡的。

子任务

子任务 数据范围 分值
1 $n \le 3$ 4
2 $n \le 15$ 13
3 最多只有一种颜色出现的次数为奇数 18
4 所有颜色出现的次数相等 23
5 $k \le 15$ 15
6 无附加限制 27

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.