Papyrus在审判你 • 1个月前
#include <bits/stdc++.h>
using namespace std;
int a[200025] = {0};
int main() {
int n, b;
cin >> n >> b;
long long int result = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
int l = 0, r = 0;
for (int i = 0; i < n; i++) {
while (a[l] < a[i] - b && l < n) {
l++;
}
if (r <= l)
r = l;
while (a[r] == a[i] - b && r < n) {
r++;
}
result += r - l;
}
cout << result << endl;
return 0;
}
评论:
请先登录,才能进行评论