P1102 A-B 数对
给出一串正整数数列以及一个正整数 C,要求计算出所有满足 A−B=C 的数对的个数(不同位置的数字一样的数对算不同的数对)。
#include <bits/stdc++.h>
using namespace std;
int x[200001];
int main()
{
int n, c;
long long ans = 0;
scanf("%d%d", &n, &c);
for (int i = 0; i < n; i++)
scanf("%d", x + i);
sort(x, x + n);
for (int i = 0; i < n; i++)
{
int a = x[i] + c; //x[i]相当于B
ans += upper_bound(x, x + n, a) - lower_bound(x, x + n, a);
}
printf("%lld\n", ans);
return 0;
}
