# 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 $ problems you are going to set, problem $ i $ having difficulty $ 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 $ (less or equal than $ 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 $ ( $ 1 \leq t \leq 1000 $ ) — the number of test cases.
The first line of each test case contains two positive integers $ n $ ( $ 1 \leq n \leq 2 \cdot 10^5 $ ) and $ k $ ( $ 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 $ space-separated integers $ a_i $ ( $ 1 \leq a_i \leq 10^9 $ ) — the difficulty of each problem.
Note that the sum of $ n $ over all test cases doesn't exceed $ 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 $ problems and construct a set using problems with the difficulties $ [4, 5, 6] $ , with difficulties between adjacent problems equal to $ |5 - 4| = 1 \leq 1 $ and $ |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 $ .
1.题目大意:其实就是让你找出需要将相邻数绝对值不超过k最少需要删除的数的数量。
2.解题思想:首先想一些边界情况,比如只有一个数时,那么不需要删除,即结果为0.
解题思路为:首先将数进行排序,因为有序的数相邻数绝对值才会最小。然后也是最重要的思想,使用逆向思维,让你求需要删除的最小数,也就是说只需要求符合条件的留下来的数的最大数量,剩下的也就是删除的最小数。想明白这一点,在编程方面就简单了,只需要求出相邻满足条件的数列最大长度即可。代码如下:
// 蓝桥杯.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
int k;
int max = 1;//设一个可更新的max,用于与c比较最后记录满足题意的最大数列长。
cin >> n;
cin >> k;
vector <int> a;
int c = 1;//记录满足题意的数列长
int cut=0;
for (int i = 0; i < n; i++)
{
int num;
cin >> num;
a.push_back(num);
}
sort(a.begin(), a.end());//先排序
for (int i = 0; i < n; i++)
{
if (i == 0) continue;
else
{
if (a[i] - a[i - 1] <= k) c++;//满足题意的数列长
else//如果遇到不满足的情况,则c停止记录,此时c为当前满足题意的数列长
{
c = 1;//千万别忘了更新c
}
if (max < c)//每次都进行比较,因为上面的便利当遍历到尾可能遇到仍然满足题意进而造成没有与max相比较,造成c最后比max大但是没有更新max,为了排除这种情况,最好再次进行判断。
{
max = c;//跌代max求最长。
}
}
}
cout << n-max<<endl;//将满足题意的最大数列长求出后,使用总数列长减去max即可得到需要删除的最小数量。
a.clear();
}
}
总结:
实际上这道题只需想出留下最大就会删除最少就简单了,如果单从题目出发求最大删除数就会很迷茫。