25分,求调
查看原帖
25分,求调
1451996
封禁用户楼主2025/1/16 10:32
#include<bits/stdc++.h> 
using namespace std;
struct node{
	int x;
	int cnt;
}b[2000001];
long long a[2000001]; 
int main(){
	int n,c,num=0;
	long long ans=0;
	cin>>n>>c;
	for(int i=1;i<+n;i++)
		cin>>a[i];
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++){
		if(num==0 || a[i]!=b[num].x){
			num++;
			b[num].x=a[i];
			b[num].cnt=1;
		}
		else if(a[i]==b[num].x)
			b[num].cnt++;
	}
	int pos1=1,pos2=1;
	while(pos1<=n){
		while(b[pos2].x-b[pos1].x<c && pos2<=n) pos2++;
		if(b[pos2].x-b[pos1].x==c) ans+=1ll*b[pos2].cnt*b[pos1].cnt;
		pos1++;
	}
	cout<<ans;
	return 0;
}
2025/1/16 10:32
加载中...