求助洛谷 1 月月赛 III & JROI R5 第一题
  • 板块学术版
  • 楼主南瓜桐
  • 当前回复5
  • 已保存回复5
  • 发布时间2022/1/31 10:00
  • 上次更新2023/10/28 09:59:49
查看原帖
求助洛谷 1 月月赛 III & JROI R5 第一题
439327
南瓜桐楼主2022/1/31 10:00
#include<iostream>
#include<vector>
#include<cmath> 
using namespace std;
int n,x,t;
long long stime = 0;
const int maxn=1e+7 + 3;
vector < vector<int> >v(maxn+1);
int mykey(int wzl){
    return abs(wzl%(maxn+1))+1;
}
void h_insert(int a){
    int num=mykey(a);
    v[num].push_back(a);
}
bool h_find(int a){
    int num=mykey(a);
    for(int i=0;i<v[num].size();++i){
        if(v[num][i]==a){
            return true;
        }
    }
    return false;
}
int main(){
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n;
    do{
        cin>>x>>t;
        if(h_find(x) == false && t > 1){
            h_insert(x);
            stime += t;
        }
    }while(--n);
    cout<<stime;
    return 0;
}

我是用的哈希做的,我知道这题可以直接开一个10^7的标记数组,那既然标记数组可以开10^7那为什么vector就不能?
我把maxn从10^7换到10^5就A了(这是为什么啊?)我只对10^5取余那么最好情况下也得是10^2个哈希冲突,那么总体时间复杂度就是10^7 * 10^2 = 10^9,在不应该TLE吗qaq?

2022/1/31 10:00
加载中...