#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
ll a[N];
int main() {
ll n, c, ans = 0;
cin >> n >> c;
for (int i = 1; i <= n; i++)
cin >> a[i];
sort(a+1,a+1+n);//排序,使其成单调上升的形式
ll l = 1, r1 = 1, r2 = 1;//l用来遍历数组中的数,r1,r2来找满足条件的区间
for (; l <= n; l++) {
while (r1 <= n && a[r1] - a[l] <= c)
r1++;
while (r2 <= n && a[r2] - a[l] < c)
r2++;
if (a[r2] - a[l] == c && a[r1 - 1] - a[l] == c && r1 - 1 >= 1)
ans += r1 - r2;
}
cout << ans << endl;
return 0;
}
A-B 数对
最新推荐文章于 2024-06-28 08:48:19 发布