CF2146A.Equal Occurrences

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

We call an array balanced if and only if the numbers of occurrences of any of its elements are the same. For example, [1,1,3,3,6,6][1,1,3,3,6,6] and [2,2,2,2][2,2,2,2] are balanced, but [1,2,3,3][1,2,3,3] is not balanced (the numbers of occurrences of elements 11 and 33 are different). Note that an empty array is always balanced.

You are given a non-decreasing array aa consisting of nn integers. Find the length of its longest balanced subsequence∗^{\text{∗}}.

∗^{\text{∗}}A sequence bb is a subsequence of a sequence aa if bb can be obtained from aa by the deletion of several (possibly, zero or all) element from arbitrary positions.

我们称一个数组是平衡的,当且仅当其中任意元素的出现次数均相同。例如,[1,1,3,3,6,6][1,1,3,3,6,6] 和 [2,2,2,2][2,2,2,2] 是平衡的,但 [1,2,3,3][1,2,3,3] 不是平衡的(元素 11 与 33 的出现次数不同)。注意:空数组始终是平衡的。

给定一个长度为 nn 的非递减整数数组 aa。求其最长平衡子序列∗^{\text{∗}} 的长度。

∗^{\text{∗}} 序列 bb 是序列 aa 的一个子序列,当且仅当 bb 可通过从 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 a single integer nn (1≤n≤1001 \leq n \leq 100) — the length of aa.

The second line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n (1≤a1≤a2≤⋯≤an≤n1\le a_1\le a_2\le \cdots \le a_n\le n) — the elements of aa.

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

每个测试用例的第一行包含一个整数 nn(1≤n≤1001 \leq n \leq 100)—— 数组 aa 的长度。

第二行包含 nn 个整数 a1,a2,…,ana_1,a_2,\ldots,a_n(1≤a1≤a2≤⋯≤an≤n1\le a_1\le a_2\le \cdots \le a_n\le n)—— 数组 aa 的元素。

输出格式

For each test case, output a single integer — the length of the longest balanced subsequence of aa.

对于每个测试用例,输出一个整数——数组 aa 的最长平衡子序列的长度。

输入输出样例

  • 输入#1

    4
    5
    1 1 4 4 4
    2
    1 2
    15
    1 1 1 1 1 2 2 2 2 3 3 3 4 4 5
    5
    3 3 3 3 3

    输出#1

    4
    2
    9
    5

说明/提示

In the first test case, the whole array a=[1,1,4,4,4]a = [1, 1, 4, 4, 4] is not balanced because the number of occurrences of element 11 is 22, while the number of occurrences of element 44 is 33, which are not equal. The subsequence [1,1,4,4][1, 1, 4, 4] is balanced because the numbers of occurrences of elements 11 and 44 are both 22. Thus, the length of the longest balanced subsequence of aa is 44.

In the second test case, the whole array a=[1,2]a = [1, 2] is already balanced, so the length of the longest balanced subsequence of aa is 22.

In the third test case, the longest balanced subsequence of aa is [1,1,1,2,2,2,3,3,3][1,1,1,2,2,2,3,3,3].

In the fourth test case, the whole array a=[3,3,3,3,3]a = [3, 3, 3, 3, 3] is already balanced, so the length of the longest balanced subsequence of aa is 55.

在第一个测试用例中,整个数组 a=[1,1,4,4,4]a = [1, 1, 4, 4, 4] 不是平衡的,因为元素 11 的出现次数为 22,而元素 44 的出现次数为 33,二者不相等。子序列 [1,1,4,4][1, 1, 4, 4] 是平衡的,因为元素 11 和 44 的出现次数均为 22。因此,数组 aa 的最长平衡子序列的长度为 44。

在第二个测试用例中,整个数组 a=[1,2]a = [1, 2] 已经是平衡的,所以数组 aa 的最长平衡子序列的长度为 22。

在第三个测试用例中,数组 aa 的最长平衡子序列是 [1,1,1,2,2,2,3,3,3][1,1,1,2,2,2,3,3,3]。

在第四个测试用例中,整个数组 a=[3,3,3,3,3]a = [3, 3, 3, 3, 3] 已经是平衡的,所以数组 aa 的最长平衡子序列的长度为 55。

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

首页