Forbidden Difference


Submit solution

Points: 500
Time limit: 1.0s
Memory limit: 256M

Author:
Problem type
Allowed languages
C, C++
https://atcoder.jp/contests/abc403/tasks/abc403_d

给定一个长度为 \(𝑁\) 的整数序列 \(A=(A_1,A_2,...,A_N)\) 和一个非负整数 \(D\)。

你可以从序列 \(A\) 中删除一些元素,得到一个新序列 \(B\),使其满足以下条件:

对于所有的 \( 1 \le i < j \le \left| B \right| \) ,都有:\(\left| B_i - B_j \right| \neq D\)

请你求出:最少需要删除多少个元素,才能使得到的序列 \(B\) 满足上述条件。

输入格式
N D
A_1 A_2 ... A_N

\(1 \le N \le 2 \times 10^5 \)
\( 0 \le D \le 10^6 \)
\( 0 \le A_i \le 10^6 \)

输出格式

输出一个整数,表示最少需要删除的元素个数。

示例 1:

输入:

5 2
3 1 4 1 5

输出:

1
解释:

删除元素 3,得到的序列 B = [1, 4, 1, 5] 中任意两个数的差都不等于 2,满足条件。

示例 2:

输入:

4 3
1 6 1 8

输出:

0
解释:

原始序列已经满足条件,无需删除任何元素。

示例 3:

输入:

10 3
1 6 2 10 2 3 2 10 6 4

输出:

2

Comments

There are no comments at the moment.