AtCoder Grand Contest 059
B
Arrange Your Balls
AtCoder
⌀ 1887
原题
← AC →
翻译中…

Problem Statement

翻译中…

You have $N$ balls of colors $C_1, C_2, \ldots, C_N$. Here, all colors are represented by an integer between $1$ and $N$ inclusive. You want to arrange the balls on a circle. After you do that, you will count the number of pairs of colors $(X, Y)$ such that $X < Y$ and there exist two adjacent balls of colors $X$ and $Y$.

翻译中…

Find an arrangement that minimizes the number of such pairs. If there are many such arrangements, find any of them.

翻译中…

For example, for balls of colors $1, 1, 2, 3$, if we arrange them as $1, 1, 2, 3$, we get $3$ pairs: $(1, 2), (2, 3), (1, 3)$. If we arrange them as $1, 2, 1, 3$, we get only $2$ pairs: $(1, 2), (1, 3)$.

翻译中…

Solve $T$ test cases for each input file.

翻译中…

Constraints

翻译中…
  • $1 \le T \le 5 \cdot 10^4$
  • $3 \le N \le 2 \cdot 10^5$
  • $1 \le C_i \le N$
  • The sum of $N$ in one input file doesn't exceed $2\cdot 10^5$.
  • All values in the input are integers.
翻译中…

Input

翻译中…

Input is given from Standard Input in the following format:

翻译中…

T
case_1
case_2
\vdots
case_T

翻译中…

Each case is in the following format:

翻译中…

N
C_1 C_2 \ldots C_N

翻译中…

Output

翻译中…

For each test case, output your answer in the following format:

翻译中…

A_1 A_2 \ldots A_N

翻译中…

Here, $A_i$ is the color of the $i$-th ball (in clockwise order) on the circle in your arrangement.

翻译中…

$(A_1, A_2, \ldots, A_N)$ should be a permutation of $(C_1, C_2, \ldots, C_N)$, and the number of pairs of colors $(X, Y)$ such that $X < Y$ and there exist two adjacent balls of colors $X$ and $Y$, should be minimized.

翻译中…

If multiple solutions exist, any of them will be accepted.

翻译中…

样例

Input #1
3
3
1 2 3
4
1 2 1 3
5
2 2 5 3 3
Output #1
1 2 3
2 1 3 1
3 3 2 5 2
题目信息
题号
agc059_b
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。