未过代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
unordered_map<LL,LL> Hash;
LL n,m,ans;
int main(){
scanf("%lld%lld",&n,&m);
for(int i=1;i<=n;i++){
LL a;
scanf("%lld",&a);
Hash[a]++;
}
for(auto it=Hash.begin();it!=Hash.end();it++){
LL a=it->first;
ans+=Hash[a+m]*Hash[a];
}
printf("%lld\n",ans);
return 0;
}
改了之后AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=2e5+10;
unordered_map<int,int> Hash;
int n,m,a[N];
LL ans;
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
Hash[a[i]]++;
a[i]-=m;
}
for(int i=1;i<=n;i++)
ans+=LL(Hash[a[i]]);
printf("%lld\n",ans);
return 0;
}