CF2267B.Fashionable Array

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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][1, 1, 2] is 11, and the mode of the array [3,4][3, 4] is 44.

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

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

  • The prefix of length 11 is [3][3]. The mode of this prefix is 33.
  • The prefix of length 22 is [3,2][3, 2]. The mode of this prefix is 33.
  • The prefix of length 33 is [3,2,2][3, 2, 2]. The mode of this prefix is 22.

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

数组的众数是指在数组中出现次数最多的数。如果存在多个出现次数最多的数,则众数为其中最大的那个数。例如,数组 [1,1,2][1, 1, 2] 的众数是 11,而数组 [3,4][3, 4] 的众数是 44。

给定一个由 nn 个整数组成的数组 aa。你可以以任意顺序重新排列(即置换)数组 aa 中的数字。你的任务是重排数组 aa 中的数字,使得所有前缀的众数之和最大。

例如,数组 [2,3,2][2, 3, 2] 可被重排为 [3,2,2][3, 2, 2]。此时,所有前缀的众数之和计算如下:

  • 长度为 11 的前缀是 [3][3],其众数为 33。
  • 长度为 22 的前缀是 [3,2][3, 2],其众数为 33。
  • 长度为 33 的前缀是 [3,2,2][3, 2, 2],其众数为 22。

因此,众数之和为 3+3+2=83 + 3 + 2 = 8。可以证明,对于该数组 aa 的这种排列方式,所得答案为最大值。

输入格式

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

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

The second line of each test case contains nn integers a1,a2,…ana_1, a_2, \ldots a_n (1≤ai≤1001 \le a_i \le 100) — the elements of the array.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤5001 \le t \le 500)。随后是测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤1001 \le n \le 100)—— 数组的大小。

每个测试用例的第二行包含 nn 个整数 a1,a2,…ana_1, a_2, \ldots a_n(1≤ai≤1001 \le a_i \le 100)—— 数组的元素。

输出格式

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.

对于每个测试用例,输出一个新数组,使得该数组所有前缀的众数之和最大。如果存在多个最优解,输出其中任意一个即可。

输入输出样例

  • 输入#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

    输出#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

说明/提示

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

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

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

  • The prefix of length 11 is [4][4]. The mode of this prefix is 44.
  • The prefix of length 22 is [4,1][4, 1]. The mode of this prefix is 44.
  • The prefix of length 33 is [4,1,3][4, 1, 3]. The mode of this prefix is 44.
  • The prefix of length 44 is [4,1,3,2][4, 1, 3, 2]. The mode of this prefix is 44.
  • The prefix of length 55 is [4,1,3,2,2][4, 1, 3, 2, 2]. The mode of this prefix is 22.

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

第一个测试用例已在题目描述中给出。

在第二个测试用例中,一种可行的排列是 [4,4,3,2,1,1][4, 4, 3, 2, 1, 1]。每个前缀的众数均为 44。

在第三个测试用例中,排列 [4,1,3,2,2][4, 1, 3, 2, 2] 是一个合法答案。各前缀的众数如下:

  • 长度为 11 的前缀为 [4][4],其众数为 44。
  • 长度为 22 的前缀为 [4,1][4, 1],其众数为 44。
  • 长度为 33 的前缀为 [4,1,3][4, 1, 3],其众数为 44。
  • 长度为 44 的前缀为 [4,1,3,2][4, 1, 3, 2],其众数为 44。
  • 长度为 55 的前缀为 [4,1,3,2,2][4, 1, 3, 2, 2],其众数为 22。

因此,众数之和为 4+4+4+4+2=184 + 4 + 4 + 4 + 2 = 18。可以证明该排列使得众数之和达到最大。

输入解题思路,AI测评打分。不知道怎么写?

首页