解题思路:对于任意一个同学,如果与他分值最近的同学都不满足条件,则其他的也不满足。由此我们可以对分值进行排序然后进行模拟即可
C++代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 10;
int a[N], n, m;
signed main()
{
while (cin >> n >> m)
{
for (int i = 1; i <= n; i ++ ) cin >> a[i];
sort(a + 1, a + n + 1);
int cnt = 0;
for (int i = 1; i < n; i ++ )
{
if (a[i] + m > a[i + 1])
{
cnt ++ ;
i ++ ;
}
}
cout << cnt << endl;
}
return 0;
}