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] and [2,2,2,2] are balanced, but [1,2,3,3] is not balanced (the numbers of occurrences of elements 1 and 3 are different). Note that an empty array is always balanced.
You are given a non-decreasing array a consisting of n integers. Find the length of its longest balanced subsequence∗.
∗A sequence b is a subsequence of a sequence a if b can be obtained from a by the deletion of several (possibly, zero or all) element from arbitrary positions.
我们称一个数组是平衡的,当且仅当其中任意元素的出现次数均相同。例如,[1,1,3,3,6,6] 和 [2,2,2,2] 是平衡的,但 [1,2,3,3] 不是平衡的(元素 1 与 3 的出现次数不同)。注意:空数组始终是平衡的。
给定一个长度为 n 的非递减整数数组 a。求其最长平衡子序列∗ 的长度。
∗ 序列 b 是序列 a 的一个子序列,当且仅当 b 可通过从 a 中任意位置删除若干(可能为零个或全部)元素而得到。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤500). The description of the test cases follows.
The first line of each test case contains a single integer n (1≤n≤100) — the length of a.
The second line contains n integers a1,a2,…,an (1≤a1≤a2≤⋯≤an≤n) — the elements of a.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤500)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤100)—— 数组 a 的长度。
第二行包含 n 个整数 a1,a2,…,an(1≤a1≤a2≤⋯≤an≤n)—— 数组 a 的元素。
输出格式
For each test case, output a single integer — the length of the longest balanced subsequence of a.
对于每个测试用例,输出一个整数——数组 a 的最长平衡子序列的长度。
输入输出样例
输入#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] is not balanced because the number of occurrences of element 1 is 2, while the number of occurrences of element 4 is 3, which are not equal. The subsequence [1,1,4,4] is balanced because the numbers of occurrences of elements 1 and 4 are both 2. Thus, the length of the longest balanced subsequence of a is 4.
In the second test case, the whole array a=[1,2] is already balanced, so the length of the longest balanced subsequence of a is 2.
In the third test case, the longest balanced subsequence of a is [1,1,1,2,2,2,3,3,3].
In the fourth test case, the whole array a=[3,3,3,3,3] is already balanced, so the length of the longest balanced subsequence of a is 5.
在第一个测试用例中,整个数组 a=[1,1,4,4,4] 不是平衡的,因为元素 1 的出现次数为 2,而元素 4 的出现次数为 3,二者不相等。子序列 [1,1,4,4] 是平衡的,因为元素 1 和 4 的出现次数均为 2。因此,数组 a 的最长平衡子序列的长度为 4。
在第二个测试用例中,整个数组 a=[1,2] 已经是平衡的,所以数组 a 的最长平衡子序列的长度为 2。
在第三个测试用例中,数组 a 的最长平衡子序列是 [1,1,1,2,2,2,3,3,3]。
在第四个测试用例中,整个数组 a=[3,3,3,3,3] 已经是平衡的,所以数组 a 的最长平衡子序列的长度为 5。
输入解题思路,AI测评打分。不知道怎么写?