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;
}