献给cyt同学
2024-03-30 22:07:24
发布于:黑龙江
21阅读
0回复
0点赞
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
map <int, int> h;
const int N = 2e5 + 10;
int n, m, x, a[N], ans;
int main(){
cin >> n >> m;
for (int i = 0; i < n; i ++ ){
cin >> a[i];
h[a[i]] ++;
}
sort(a, a + n);
for (int i = 0; i < n; i ++ ){
int l = 0, r = n - 1;
int x = a[i] + m;
while (l < r){
int mid = (l + r) / 2;
if(x <= a[mid]) r = mid;
else l = mid + 1;
}
if(a[l] == x) ans += h[a[l]];
}
cout << ans << endl;
return 0;
}
这里空空如也
有帮助,赞一个