Codeforces Round 1052 A. Equal Occurrences (相同出现次数)

A. Equal Occurrences

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.)

Input
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.

Output
For each test case, output a single integer — the length of the longest balanced subsequence of a.
Example
Input
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
Output
4
2
9
5

Note
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.
 

题意:

例如:1 1 1 1 1 2 2 2 2 3 3 3 4 4 5 中,求出现相同次数是最多的数。

第一步:统计每个不同的数,出现次数是多少:【5-4-3-2-1】

第二步:统计出现次数一样或者包含的总次数:【5-8-9-8】

例如:

1出现5次,其他都不包含5次,所以是5,

2出现了4次,1也出现了4+1次,那么就是 = 4+4=8次,

3出现3次,前面5,4都包含3次,那么总次数=3+3+3=9,

4出现2次,前面都包含,那么总次数就是:2+2+2+2=8,

5出现1次,前面都包含,总次数就是5,

那么最大是9,就是111 222 333,一共出现次数是3+3+3=9

理解题意后,然后开始编程

思路:

1.首先找到每个不同数字出现次数。

2.找到每个不同数字出现的次数与其他被包含(<=)的出现次数总和

3.从以上总次数里,找到最大次数,就是题意要求的结果。

耗费了3小时,主要是开始没有完全理解。

#include <iostream>
#include <vector>
using namespace std;

/*
2025-10-01
*/
int main()
{
	int t = 0;
	cin >> t;
	while (t--)
	{
		int n = 0;
		cin >> n;
		int len = n;
		int *list = new int[len];
		for (int i = 0; i < len; i++)
		{
			cin >> list[i];
		}

		//total:times of every number
		vector<pair<int, int>> clist;
		pair<int, int> p1(0, 1);
		for (int i = 0; i < len; i++)
		{
			p1.first = list[i];
			for (int j = i + 1; j < len; j++)
			{
				if (list[i] == list[j])
				{
					p1.second++;
				}
			}
			clist.push_back(p1);
			//cout << p1.first << "::" << p1.second << endl;
			i += (p1.second - 1);
			p1.second = 1;

		}
		//total:tims
		vector<int> sumlist;
		int cout1 = 0;
		int curr_cout = 0;
		for (int i = 0; i < clist.size(); i++)
		{
			curr_cout = clist[i].second;
			for (int j = 0; j < clist.size(); j++)
			{
				if (curr_cout <= clist[j].second)
				{
					cout1 += clist[i].second;
				}
			}
			sumlist.push_back(cout1);
			cout1 = 0;
		}

		//get max
		int max_count = 0;		
		for (int i = 0; i < sumlist.size(); i++)
		{
			if (max_count < sumlist[i])
			{
				max_count = sumlist[i];
				
			}
		}
		cout <<max_count << endl;
	}
	return 0;
}

/*
A. Equal Occurrences
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.)

Input
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)

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

Input
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

Output
4
2
9
5

Note
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.
*/

    

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值