防cf上unordered_map被卡

题目链接:

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

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值