D. Balanced Round

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are the author of a Codeforces round and have prepared n� problems you are going to set, problem i� having difficulty ai��. 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� (less or equal than k�).

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

Input

The first line contains a single integer t� (1≤t≤10001≤�≤1000) --- the number of test cases.

The first line of each test case contains two positive integers n� (1≤n≤2⋅1051≤�≤2⋅105) and k� (1≤k≤1091≤�≤109) --- the number of problems, and the maximum allowed absolute difference between consecutive problems.

The second line of each test case contains n� space-separated integers ai�� (1≤ai≤1091≤��≤109) --- the difficulty of each problem.

Note that the sum of n� over all test cases doesn't exceed 2⋅1052⋅105.

Output

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.

Example

input

Copy

复制代码

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

output

Copy

复制代码
2
0
5
0
3
1
4

Note

For the first test case, we can remove the first 22 problems and construct a set using problems with the difficulties [4,5,6][4,5,6], with difficulties between adjacent problems equal to |5−4|=1≤1|5−4|=1≤1 and |6−5|=1≤1|6−5|=1≤1.

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

解题说明:此题是一道数学题,分析后能发现直接对数列排序,然后再遍历一遍找出符合两个数之间差 <= k 的元素数量是多少, 再用总数量减去符合两个数之间差 <= k 的元素最长连续数量。

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
int t, n, k, ans, temp, a[200000];
int main() 
{
	cin >> t;
	while (t--)
	{
		cin >> n >> k;
		for (int i = 0; i < n; i++)
		{
			cin >> a[i];
		}
		sort(a, a + n);
		temp = 1;
		ans = 1;
		for (int i = 1; i < n; i++) 
		{
			if (a[i] - a[i - 1] <= k)
			{
				temp++;
			}
			else
			{
				temp = 1;
			}
			ans = max(ans, temp);
		}
		cout << n - ans << '\n';
	}
	return 0;
}
相关推荐
Cestb0n3 分钟前
某果app 加密校验算法逆向分析
python·算法·逆向安全
机器学习之心16 分钟前
MATLAB基于近红外光谱检测的菠萝含水率预测(多种预处理+PLS)
人工智能·算法·matlab·近红外光谱检测
程序员-King.36 分钟前
day166—递归—多边形三角剖分的最低得分(LeetCode-1039)
算法·leetcode·深度优先·动态规划·递归
夏鹏今天学习了吗38 分钟前
【LeetCode热题100(94/100)】下一个排列
算法·leetcode·职场和发展
AI科技星1 小时前
光的几何起源:从螺旋时空到量子现象的完全统一
开发语言·人工智能·线性代数·算法·机器学习
q_35488851531 小时前
机器学习:Python地铁人流量数据分析与预测系统 基于python地铁数据分析系统+可视化 时间序列预测算法 ✅
大数据·人工智能·python·算法·机器学习·信息可视化·数据分析
永远都不秃头的程序员(互关)2 小时前
【K-Means深度探索(十二)】K-Means项目实战:从数据到决策的完整工作流!
算法·机器学习·kmeans
散峰而望2 小时前
【基础算法】高精度运算深度解析与优化
数据结构·c++·算法·链表·贪心算法·推荐算法
一起养小猫2 小时前
LeetCode100天Day16-跳跃游戏II与H指数
算法·游戏
mit6.8242 小时前
两个有序集合|状态分析
算法