[Codeforces 985.E] Pencils and Boxes(dp,前缀和优化)

E. Pencils and Boxes

time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome world, where everything is of the same color and only saturation differs. This pack can be represented as a sequence $a_1, a_2, …, a_n$ of n integer numbers — saturation of the color of each pencil. Now Mishka wants to put all the mess in the pack in order. He has an infinite number of empty boxes to do this. He would like to fill some boxes in such a way that:

  • Each pencil belongs to exactly one box;
  • Each non-empty box has at least $k$ pencils in it;
  • If pencils $i$ and $j$ belong to the same box, then $|a_i - a_j| ≤ d$, where $|x|$ means absolute value of $x$. Note that the opposite is optional, there can be pencils $i$ and $j$ such that $|a_i - a_j| ≤ d$ and they belong to different boxes.

Help Mishka to determine if it’s possible to distribute all the pencils into boxes. Print "YES" if there exists such a distribution. Otherwise print "NO".

Input

The first line contains three integer numbers $n$, $k$ and $d$ ($1 ≤ k ≤ n ≤ 5·10^5$, $0 ≤ d ≤ 10^9$) — the number of pencils, minimal size of any non-empty box and maximal difference in saturation between any pair of pencils in the same box, respectively.

The second line contains n integer numbers $a_1, a_2, …, a_n$ ($1 ≤ a_i ≤ 10^9$) — saturation of color of each pencil.

Output

Print "YES" if it’s possible to distribute all the pencils into boxes and satisfy all the conditions. Otherwise print "NO".

Examples

Input Output
6 3 10
7 2 7 7 4 2
YES
6 2 3
4 5 3 13 4 10
YES
3 2 5
10 16 22
NO

Note

In the first example it is possible to distribute pencils into $2$ boxes with $3$ pencils in each with any distribution. And you also can put all the pencils into the same box, difference of any pair in it won’t exceed $10$.

In the second example you can split pencils of saturations $[4, 5, 3, 4]$ into $2$ boxes of size $2$ and put the remaining ones into another box.


解题思路

将数列排序,若有解,则一定存在一种数列划分的方式(即把连续一段铅笔放进一个盒子中)满足条件。
设 $dp[i]$ 表示前 $i$ 个铅笔能否放进盒子中($1$能,$0$不能),那么转移非常简单:只要存在一个 $dp[j]=1$,那么 $dp[i]=1$,其中 $j$ 满足 $1 \leqslant j \leqslant i - k$ 且 $a_i - a_{j+1} \leqslant d$(仔细想想为什么是 $a_{j+1}$)
可是如果遍历 $j​$ 的话复杂度是 $O(N^2)​$ 的,显然需要优化——其实我们只需要知道 $j​$ 所在的那个区间内是否有 $1​$ 即可,或者说,和大于 $0​$ 即可。所以我们可以用前缀和轻松做到 $O(1)​$ 查询。

综上:

  • dp状态:$dp[i]$ 表示前 $i$ 个铅笔能否放进盒子中($1$能,$0$不能)
  • dp方程:$dp[i] = (sum(pos-1, i - k) > 0)$,其中 $pos$ 是最小的满足 $a[i] - a[pos] \leqslant d$ 的点
  • dp顺序:由dp方程可得:从小到大枚举 $i$ 即可
  • 边界条件:这道题的边界条件有点迷……在实践过程中你会发现那些从 $1$ 开始的符合题意的区间会被判 $0$,所以边界条件可以是把它们置为 $1$。其他合法区间都可以从它们转移过去。

时间复杂度 $O(N)$


Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<cstdio>
#include<algorithm>

using namespace std;

const int N = 500005;
int n, k, d, a[N], ptl, ptr, cnt, dpSum[N];
bool dp[N]; // dp[i] represents whether we can put 1st~ith pencils into boxes

inline int sum(int l, int r){
if(l > r) return 0;
return dpSum[r] - dpSum[l-1];
}

int main(){
scanf("%d%d%d", &n, &k, &d);
for(int i = 1; i <= n; i++)
scanf("%d", &a[i]);
sort(a+1, a+n+1);
for(int i = k; i <= n; i++)
if(a[i] - a[1] <= d)
dp[i] = 1;
int pos = 1;
for(int i = 1; i <= n; i++){
while(a[i] - a[pos] > d) pos++;
dp[i] |= sum(pos-1, i - k);
dpSum[i] = dpSum[i-1] + dp[i];
}
return puts(dp[n] ? "YES" : "NO"), 0;
}