题解 | 数对计数 | 哈希表(官方:集合方法)
数对计数
https://www.nowcoder.com/practice/7d05171e7e0e4c6086be233769e01d94
#include <iostream>
#include <unordered_map>
using namespace std;
using ll=long long;
int main(){
ios::sync_with_stdio(false);cin.tie(nullptr);
int N,C;cin>>N>>C;
unordered_map<int,int> ump;
for(int i=0;i<N;++i){
int num;cin>>num;
++ump[num];
}
ll cnt=0;
for(const auto& u:ump){
auto it=ump.find(C+u.first);
if(it!=ump.end()) cnt+=(ll)u.second*(it->second);
}
cout<<cnt<<'\n';
return 0;
}
查看8道真题和解析