第 1123 场 Codeforces Round(Div. 2)
B
时髦数组
Fashionable Array
Codeforces
800
原题
← AC →

时髦数组AI (CI翻译)

The mode of an array — is the number that appears the maximum number of times in the array. If several numbers appear the maximum number of times, the mode is the largest among them. For example, the mode of the array $[1, 1, 2]$ is $1$, and the mode of the array $[3, 4]$ is $4$.

众数是数组中出现次数最多的数。如果有多个数出现次数最多,则众数是其中最大的数。例如,数组 $[1, 1, 2]$ 的众数是 $1$,数组 $[3, 4]$ 的众数是 $4$。AI (CI翻译)

You are given an array $a$ consisting of $n$ integers. You may arbitrarily permute the numbers in array $a$ in any order. Your task — is to rearrange the numbers in array $a$ so that the sum of the modes over all prefixes of the array is maximized.

给定一个由 $n$ 个整数构成的数组 $a$。你可以任意重排数组 $a$ 中的数。你的任务是重新排列数组 $a$,使该数组所有前缀的众数之和最大。AI (CI翻译)

For example, the array $[2, 3, 2]$ can be rearranged as $[3, 2, 2]$. Then the sum of the modes over all prefixes is determined as follows:

例如,可以将数组 $[2, 3, 2]$ 重排为 $[3, 2, 2]$。此时,所有前缀的众数之和计算如下:AI (CI翻译)

  • The prefix of length $1$ is $[3]$. The mode of this prefix is $3$.
  • The prefix of length $2$ is $[3, 2]$. The mode of this prefix is $3$.
  • The prefix of length $3$ is $[3, 2, 2]$. The mode of this prefix is $2$.
  • 长度为 $1$ 的前缀是 $[3]$,其众数是 $3$。
  • 长度为 $2$ 的前缀是 $[3, 2]$,其众数是 $3$。
  • 长度为 $3$ 的前缀是 $[3, 2, 2]$,其众数是 $2$。
AI (CI翻译)

Thus, the sum of the modes is $3 + 3 + 2 = 8$. It can be proven that for this arrangement of array $a$, the answer is maximal.

因此,众数之和为 $3 + 3 + 2 = 8$。可以证明,对于数组 $a$ 的这种排列,答案最大。AI (CI翻译)

Input

输入格式

AI (CI翻译)

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 500$). The description of the test cases follows.

每个测试文件包含多组测试用例。第一行包含测试用例数 $t$($1 \le t \le 500$)。接下来是各测试用例的描述。AI (CI翻译)

The first line of each test case contains one integer $n$ ($1 \le n \le 100$) — the size of the array.

每个测试用例的第一行包含一个整数 $n$($1 \le n \le 100$),表示数组的长度。AI (CI翻译)

The second line of each test case contains $n$ integers $a_1, a_2, \ldots a_n$ ($1 \le a_i \le 100$) — the elements of the array.

每个测试用例的第二行包含 $n$ 个整数 $a_1, a_2, \ldots a_n$($1 \le a_i \le 100$),表示数组中的元素。AI (CI翻译)

Output

输出格式

AI (CI翻译)

For each test case, output a new array whose sum of the modes over all prefixes is maximal. If there are several optimal answers, output any of them.

对于每个测试用例,输出一个新数组,使其所有前缀的众数之和最大。如果有多个最优答案,输出任意一个。AI (CI翻译)

Note

说明

AI (CI翻译)

The first test case is described in the statement of the problem.

第一个测试用例已在题目描述中给出。AI (CI翻译)

In the second test case, one suitable arrangement is $[4, 4, 3, 2, 1, 1]$. The mode of each prefix is $4$.

对于第二个测试用例,一种符合要求的排列是 $[4, 4, 3, 2, 1, 1]$。每个前缀的众数都是 $4$。AI (CI翻译)

In the third test case, the arrangement $[4, 1, 3, 2, 2]$ is a valid answer. The mode of each prefix:

对于第三个测试用例,排列 $[4, 1, 3, 2, 2]$ 是一个有效答案。各前缀的众数如下:AI (CI翻译)

  • The prefix of length $1$ is $[4]$. The mode of this prefix is $4$.
  • The prefix of length $2$ is $[4, 1]$. The mode of this prefix is $4$.
  • The prefix of length $3$ is $[4, 1, 3]$. The mode of this prefix is $4$.
  • The prefix of length $4$ is $[4, 1, 3, 2]$. The mode of this prefix is $4$.
  • The prefix of length $5$ is $[4, 1, 3, 2, 2]$. The mode of this prefix is $2$.
  • 长度为 $1$ 的前缀是 $[4]$,其众数是 $4$。
  • 长度为 $2$ 的前缀是 $[4, 1]$,其众数是 $4$。
  • 长度为 $3$ 的前缀是 $[4, 1, 3]$,其众数是 $4$。
  • 长度为 $4$ 的前缀是 $[4, 1, 3, 2]$,其众数是 $4$。
  • 长度为 $5$ 的前缀是 $[4, 1, 3, 2, 2]$,其众数是 $2$。
AI (CI翻译)

Then the sum of the modes is $4 + 4 + 4 + 4 + 2 = 18$. It can be proven that this arrangement maximizes the sum of the modes.

因此,众数之和为 $4 + 4 + 4 + 4 + 2 = 18$。可以证明,这种排列能使众数之和最大。AI (CI翻译)

样例

Input #1
7
3
2 3 2
6
4 4 2 1 3 1
5
1 3 2 4 2
4
1 1 1 2
7
1 2 3 4 5 6 7
8
1 1 4 2 3 3 3 2
8
4 3 3 3 2 1 4 1
Output #1
3 2 2
4 4 3 2 1 1
4 1 3 2 2
2 1 1 1
7 1 2 3 4 5 6
4 3 2 1 3 3 1 2
4 4 3 3 2 1 1 3
InputCopy OutputCopy
题目信息
题号
2267B
时间限制
1 s
内存限制
256 MB
标签
暴力
构造
排序

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。