Files
Data-Structure/Algorithm/Maths/P1102 A-B数对/P1102 A-B 数对 unordered_map.cpp
2025-08-30 21:59:50 +08:00

27 lines
591 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <unordered_map>
#include <vector>
#include <iostream>
//用 hash + map 的原因是不希望遍历 O(n^2)
using namespace std;
int main(){
unordered_map<long long, int> s;
vector<long long> arr;
long long n, m;
cin >> n >> m;
long long tmp;
for(int i = 0; i < n; i++){
cin >> tmp;
arr.push_back(tmp);
s[tmp]++;
}
long long need;
long long js = 0;
for(int i = 0; i < n; i++){
need = arr[i] - m;
if(s.count(need)) js += s[need];//s[need]的值是 pair.seconds
//s.emplace(make_pair(arr[i], 1));//不行如果arr[i]存在,会直接放弃插入
}
cout << js << endl;
return 0;
}