问题描述:求出A-B=C的AB数据对个数
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1e5;
int a[N];
int main() {
int n, t;
cin >> n >> t;
for (int i = 1; i <= n; i++) { cin >> a[i]; }
sort(a + 1, a + 1 + n);
int res = 0;
for (int i = 1, j = 1, k = 1; i <= n; i++) {
while (j <= n && a[j] - a[i] < t) { j++; }
while (k <= n && a[k] - a[i] <= t) { k++; }
if (a[j] - a[i] == t && a[k - 1] - a[i] == t && k - 1 >= 1) { res += k - j; }
}
cout << res << endl;
}
//6 3
//8 4 5 7 7 4
//4 4 5 7 7 8