Codeforces Round #702 (Div. 3) F - Equalize the Array

Codeforces Round #702 (Div. 3) F - Equalize the Array

题干

原题链接

Polycarp was gifted an array a of length n. Polycarp considers an array beautiful if there exists a number C, such that each number in the array occurs either zero or C times. Polycarp wants to remove some elements from the array a to make it beautiful.
For example, if n=6 and a=[1,3,2,1,4,2], then the following options are possible to make the array a array beautiful:
Polycarp removes elements at positions 2 and 5, array a becomes equal to [1,2,1,2];
Polycarp removes elements at positions 1 and 6, array a becomes equal to [3,2,1,4];
Polycarp removes elements at positions 1,2 and 6, array a becomes equal to [2,1,4];
Help Polycarp determine the minimum number of elements to remove from the array a to make it beautiful.

Input
The first line contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.
The first line of each test case consists of one integer n (1≤n≤2⋅105) — the length of the array a.
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤109) — array a.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.

Output
For each test case, output one integer — the minimum number of elements that Polycarp has to remove from the array a to make it beautiful.

Example
Input
3
6
1 3 2 1 4 2
4
100 100 4 100
8
1 2 3 3 3 2 6 6

Output
2
1
2

题解

先简单翻译一下题干
给定n元素数组a。数组a将被认为是美丽数组,当:
数组中每个数字仅出现0次或C次,C为任意数
求最少删去多少个元素才能使得数组a为美丽数组。

策略大概是把多的减少,少的删没,所以要选取一个中间的个数。好像一下子没想到什么好的选取方法,那就来枚举吧!
就统计每个数字出现了几次,然后根据出现的次数分类,然后枚举统一到每种次数的结果。

#include <bits/stdc++.h>
#include <bits/extc++.h>

using namespace std;
typedef long long ll;

//numsCount统计每种数字出现了几次<数字,次数>
map<int,int> numsCount;
//countCount统计出现first次的数字共有second个<次数,数字种数>
map<int,int> countCount;

int main()
{
    int count;
    cin>>count;
    while(count--){
		//别忘了clear
        numsCount.clear();
        countCount.clear();
        int n;
        int res = 0x3f3f3f3f;
        cin>>n;
        for(int i = 0 ; i < n ; ++i){
            int temp;
            cin>>temp;
            numsCount[temp]++;
        }
        for(auto i : numsCount){
            countCount[i.second]++;
        }
        for(auto i : countCount){
            int tempRes = 0;
            for(auto j : countCount){
                if(j == i) continue;
                tempRes += j.first > i.first ? (j.first - i.first) * j.second : j.first * j.second;
            }
            res = min(res,tempRes);
        }
        cout<<res<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值