luogu#P16357. [BalticOI 2026] Blocks
[BalticOI 2026] Blocks
Problem Description
You have wooden blocks of different colors arranged in a line. The colors of the blocks are , all between and .
A color is balanced if the average position of blocks with that color is . Note that this number is not necessarily an integer.
:::align{center}
:::
For example, if blocks are arranged in the above way, the average position of color is , the average position of color is and the average position of color is . Since , color is balanced but colors and are not.
Can you order the blocks in such a way that all colors are balanced?
Input Format
The first line contains a single integer : the number of test cases.
The following lines describe the test cases, each consisting of two lines as follows:
The first line contains two integers and : the number of blocks and the number of different colors.
The second line contains the colors of the blocks . There is at least one block of each color.
Output Format
For each test case, print "YES" if a solution exists, and "NO" otherwise. If a solution exists, describe one possible order by printing integers on another line: the color of the block in each position.
3
7 2
1 1 1 1 2 2 2
2 2
1 2
2 1
1 1
YES
1 2 2 1 1 1 2
NO
YES
1 1
Hint
Explanation
In the output of the first test case, color is balanced because its average position is , which equals . Similarly, the average position of color is . Both colors are therefore balanced.
In the second test case, neither of the two possible orders make the colors balanced.
In the output of the third test case, blocks with color appear at positions and . The average is , so color is balanced.
Constraints
- The sum of all is at most
Scoring
| Subtask | Constraints | Points |
|---|---|---|
| 1 | ||
| 2 | ||
| 3 | There is at most one color that appears an odd number of times | |
| 4 | All colors appear an equal number of times | |
| 5 | ||
| 6 | No additional constraints |