如下图所示,我们在门前一条笔直的道路上栽了
N
棵树苗。
但是,最近我们发现,如果两棵树苗的距离小于一个常数
D
,这两棵树苗的发育都会受到阻碍。因此我们决定移除一些树苗,从而使任意两棵树苗的距离都不小于
D
,并且我们希望留下的树苗越多越好。
Input
输入的第一行包含一个整数
T
(
T
> 0)
,表示一共有
T
组测试数据。
对于每组测试数据,第一行包含两个整数
N
,
D
(1 ≤
N
≤ 10
5
, 1
≤
D
≤ 10
9
)
。第二行包含
N
个整数
a
1
,
a
2
, ...,
a
N
(0 <
a
1
<
a
2
< ... <
a
N
< 10
9
)
,其中
a
i
(1 ≤
i
≤
N
)
表示第
i
棵树苗的位置。
Output
对于每组测试数据,输出我们最多可以留下多少棵树苗,并且任意两棵树苗的距离都不小于
D
。
Sample Input
5 1 3 7 2 1 3 4 2 2 3 4 7 2 1 2 3 5 6 8 9 7 4 1 2 3 5 6 8 9
Sample Output
1 2 1 4 3
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define LL long long
using namespace std;
const int MAX = 1e6+10;
const int INF = 0x3fffffff;
int a[MAX];
bool check[MAX];
int main(){
int t;
cin>>t;
while(t--){
memset(a,0,sizeof(a));
memset(check,true,sizeof(check));
int n,d;
scanf("%d%d",&n,&d);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
sort(a,a+n);
int l = 0;
for(int i=1;i<n;i++){
if(a[i]-a[l]>=d){
l = i;
}
else{
check[i] = false;
}
}
int num = 0;
for(int i=0;i<n;i++){
if(check[i]) num++;
}
cout<<num<<endl;
}
return 0;
}