mirror of
https://github.com/Kiritow/OJ-Problems-Source.git
synced 2024-03-22 13:11:29 +08:00
808996d897
5100-5199
47 lines
1.2 KiB
C++
47 lines
1.2 KiB
C++
#include<iostream>
|
|
#include<cstdio>
|
|
#include<algorithm>
|
|
using namespace std;
|
|
int main()
|
|
{
|
|
int kase;
|
|
scanf ("%d", &kase);
|
|
while (kase--)
|
|
{
|
|
int n, k;
|
|
long long ans = 0;
|
|
int num[100200], pos[100200];
|
|
scanf ("%d %d", &n, &k);
|
|
for (int i = 0; i < n; i++)
|
|
{
|
|
scanf ("%d", &num[i]);
|
|
}
|
|
sort (num, num + n);
|
|
for (int i = 0; i < n; i++)
|
|
{
|
|
if (i == 0)
|
|
{
|
|
for (int j = i + 1; j <= n; j++)
|
|
if ( (j < n && num[j] - num[i] > k) || (j == n))
|
|
{
|
|
pos[i] = j - 1;
|
|
ans += j - i - 1;
|
|
break;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for (int j = pos[i - 1]; j <= n; j++)
|
|
if ( (j < n && num[j] - num[i] > k) || j == n)
|
|
{
|
|
pos[i] = j - 1;
|
|
ans += j - i - 1;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
printf ("%I64d\n", ans);
|
|
}
|
|
return 0;
|
|
}
|