P1102 A-B 数对

题目链接

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int N=2e5+7;
ll n,c;
ll a[N];
map<ll,ll>m;

int main(){
	cin>>n>>c;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		m[a[i]]++; //记录每个数出现的次数 
                a[i]-=c; //  A-B=C--> A-C=B
	}
	ll ans=0;
	for(int i=1;i<=n;i++){
		ans+=m[a[i]]; // a[i]现在表示 B, m[a[i]]表示 B 的出现次数
	} 
	cout<<ans;
	return 0;
}

posted @ 2024-05-15 17:30  LTphy  阅读(21)  评论(0)    收藏  举报