我尝试在本题使用C++的map求解,但是问题就在于在测试点4MLE,看到讨论区从来没有出现过此情况,求帮助
代码如下:
#include <bits/stdc++.h>
typedef unsigned long long ull;
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
ull num;
ull c, ma = 0;
cin >> n >> c;
map<ull, ull> hash;
for(int i=0; i<n; i++){
cin >> num;
hash[num]++;
if(num > ma) ma = num;
}
// 遍历map
ull res = 0;
for(auto it = hash.begin(); it!= hash.end(); it++){
ull a = it->first + c;
if(a > ma) break;
else{
res += it->second * hash[ a ];
}
}
cout << res;
return 0;
}