源代码如下,在自己的ide上答案都是对的,拿给洛谷就出现异常,求求大佬解答 >3
#include <iostream>
#include <algorithm>
#define fors(a,b) for(int i=a;i<b;i++)
int read();
const int N=2e5+10;
int arr[N]={};
int main(){
int n=read(),c=read();
int cnt=0;
fors(0,n)arr[i]=read();
int l=0,r=1;
int tmpt=0;
std::sort(arr,arr+n);
while(r<=n){
while(arr[r]-arr[l]<c){r++;tmpt=r;}
while(arr[r]-arr[l]>c){
r=tmpt;
l++;
}
if(arr[r]-arr[l]==c){
if(r+1<n) cnt++,r++;
else cnt++,r=tmpt,l++;
}
}
printf("%d",cnt);
return 0;
}
int read() {
int num = 0;
char c;
bool tag = false;
while ((c = getchar()) != '-' && c != '+' && (c < '0' || c > '9') && ~c);
if (!~c)
return EOF;
if (c == '-')
tag = true;
else if (c == '+')
tag = false;
else
num = c ^ 48;
while ((c = getchar()) >= '0' && c <= '9')
num = (num << 1) + (num << 3) + (c ^ 48);
if (tag)
return -num;
return num;
}