头像

欢迎来到我的博客

分享题解与总结

P1102A-B数对

image

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int N0=2e5+7;
int a[N0];

int main()
{
	ll ans=0;
	int N,C;scanf("%d%d",&N,&C);
	for(int i=1;i<=N;++i)
		scanf("%d",a+i);
	sort(a+1,a+N+1);
	for(int i=1;i<=N;++i)
		ans+=distance(lower_bound(a+1,a+N+1,a[i]+C),upper_bound(a+1,a+N+1,a[i]+C));
	printf("%lld",ans);
	return 0;
}
posted @ 2026-02-10 20:03  king_steph1209  阅读(6)  评论(0)    收藏  举报