#P1850D. Balanced Round

Balanced Round

题目描述

你是codeforces round的出题人,现在你将设置n个问题,第i个问题的难度是ai。你将进行以下操作步骤:

  1. 从题单中移除一部分题目(移除的题目的数量可能是0)
  2. 按你想要的任何顺序重新排列剩余的问题

当且仅当任意两道连续的题目的难度之差的绝对值最多为k时(即绝对值小于等于k),这一回合(round)会被认为是平衡的。

你最少需要移除多少道题目,才能使问题的安排是平衡的?

输入格式

第一行包含一个整数t(1≤t≤1000),代表样例的数量

对于每个样例的第一行包含两个正整数n(1≤n≤2⋅10^5)和k(1≤k≤10^9),n代表初始问题的数量,k代表连续的两个问题难度之差的绝对值的最大值

对于每个样例的第二行包含n个用空格隔开的整数ai(1≤ai≤10^9),代表每个问题的难度

请注意,所有测试用例的n不超过2⋅10^5

输出格式

对于每个测试用例,输出一个正整数,代表你为了使问题的安排平衡所最少需要移除的问题的数量

说明/提示

对于第一个样例,我们可以移除前两个问题并得到一个问题的排列,其难度为【4,5,6】,连续的两个问题的难度之差的绝对值满足|5-4|=1≤1,|6-5|=1≤1

对于第二个样例,我们可以得到一个问题并将这一个问题(难度10)作为一个回合(round)

题目描述

You are the author of a Codeforces round and have prepared n n problems you are going to set, problem i i having difficulty ai a_i . You will do the following process:

  • remove some (possibly zero) problems from the list;
  • rearrange the remaining problems in any order you wish.

A round is considered balanced if and only if the absolute difference between the difficulty of any two consecutive problems is at most k k (less or equal than k k ).

What is the minimum number of problems you have to remove so that an arrangement of problems is balanced?

输入格式

The first line contains a single integer t t ( 1t1000 1 \leq t \leq 1000 ) — the number of test cases.

The first line of each test case contains two positive integers n n ( 1n2105 1 \leq n \leq 2 \cdot 10^5 ) and k k ( 1k109 1 \leq k \leq 10^9 ) — the number of problems, and the maximum allowed absolute difference between consecutive problems.

The second line of each test case contains n n space-separated integers ai a_i ( 1ai109 1 \leq a_i \leq 10^9 ) — the difficulty of each problem.

Note that the sum of n n over all test cases doesn't exceed 2105 2 \cdot 10^5 .

输出格式

For each test case, output a single integer — the minimum number of problems you have to remove so that an arrangement of problems is balanced.

样例 #1

样例输入 #1

7
5 1
1 2 4 5 6
1 2
10
8 3
17 3 1 20 12 5 17 12
4 2
2 4 6 8
5 3
2 3 19 10 8
3 4
1 10 5
8 1
8 3 1 4 5 10 7 3

样例输出 #1

2
0
5
0
3
1
4

提示

For the first test case, we can remove the first 2 2 problems and construct a set using problems with the difficulties [4,5,6] [4, 5, 6] , with difficulties between adjacent problems equal to 54=11 |5 - 4| = 1 \leq 1 and 65=11 |6 - 5| = 1 \leq 1 .

For the second test case, we can take the single problem and compose a round using the problem with difficulty 10 10 .