60pts 后四点TLE 求大佬
查看原帖
60pts 后四点TLE 求大佬
924484
looloa楼主2023/7/8 13:57

贴代码

#include <iostream>
#include <vector>
#include <algorithm>
#define MOD 17001


using std::cin;
using std::cout;
using std::vector;
using std::endl;
using std::max;


long long n, m, k, maxn;
vector <long long> arr[MOD+2];

long long hash(int num){
	return abs(num) % MOD;
}

int main(){
	cin >> n;
	
	while(n--){
		cin >> m;
		for(int i=0; i<=maxn; i++) arr[i].clear();
		maxn = 0;
		
		while(m--){
			bool flag = false;
			
			cin >> k;
			auto hashk = hash(k);
			maxn = max(maxn, hashk);
			for(auto i=0u; i<arr[hashk].size(); i++)
				if(arr[hashk][i] == k) {flag = true; break;}
			
			if(flag) continue;
			else {arr[hash(k)].push_back(k); cout << k << ' ';}
		}
		cout << endl;
	}
}

玄学,手搓哈希表为何会超时?

如何优化?劳烦斧正,感激不尽!

2023/7/8 13:57
加载中...