题目链接:
https://codeforces.com/contest/1490/problem/F
本来是都不怎么用unordered_map的,怕卡常就用了。然而cf确实不卡常,但是卡哈希。
一般来说卡哈希用双哈希就卡不了了。
这里提供一个cf大佬的文章防止卡哈希
博客地址:http://codeforces.com/blog/entry/62393%E3%80%82
需要添加:#include<chrono>
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
200+ms正常通过
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<map>
#include<chrono>
#include<set>
#include<cstdio>
#include<unordered_map>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=2e5+1000;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar(); while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
vector<LL>v;
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
unordered_map<LL,LL,custom_hash>map1;
LL a[maxn];
LL solve(LL n){
LL ans=0;
for(LL i=1;i<=n;i++){
if(!map1.count(a[i])) continue;
else{
v.push_back(map1[a[i]]);
ans+=map1[a[i]];
map1[a[i]]=0;
}
}
LL k=1e18;LL sum=0;
sort(v.begin(),v.end());
for(LL i=0;i<v.size();i++){
LL j1=v[i];
LL k1=j1;
ans-=j1;
while(j1==v[i+1]){
sum-=j1;
i++;
k1+=j1;
}
LL haha=sum+ans-j1*(v.size()-1-i);
k=min(k,haha);
sum+=k1;
}
return k;
}
int main(void)
{
cin.tie(0);std::ios::sync_with_stdio(false);
LL t;cin>>t;
while(t--){
v.clear();
map1.clear();
LL n;cin>>n;
for(LL i=0;i<n+100;i++) a[i]=0;
for(LL i=1;i<=n;i++){
cin>>a[i];
}
for(LL i=1;i<=n;i++){
map1[a[i]]++;
}
LL ans=solve(n);
cout<<ans<<"\n";
}
return 0;
}