codeforces 985 E. Pencils and Boxes

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 a1, a2, ..., an 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 |ai - aj| ≤ d, where |x| means absolute value of x. Note that the opposite is optional, there can be pencils i and j such that |ai - aj| ≤ 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·105, 0 ≤ d ≤ 109) — 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 a1, a2, ..., an (1 ≤ ai ≤ 109) — 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
6 3 10
7 2 7 7 4 2
Output
YES
Input
6 2 3
4 5 3 13 4 10
Output
YES
Input
3 2 5
10 16 22
Output
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.

【题意】 给$$$n$$$个数,可以划分成若干个部分,要求每个部分至少有$$$k$$$个数,并且每个部分中最大和最小的数差值不超过$$$d$$$。问是否有合理的方案来划分。

【分析】 暂时贴一个代码,等待被hack的命运。用搜索做的,每一个连续块选或者不选都试一下,并记录下哪些点作为起点继续往下搜索是不行的。

#include<stdio.h>
#include<algorithm>
using std::sort;
#define N_max 500005
int ipt[N_max];
int cmpv(int t1, int t2) {
	return t1 < t2;
}

int binsch(int x,int fr,int to) {
	int l = fr - 1, r = to + 1, m;
	while (l+1<r)
	{
		m = (l + r) / 2;
		if (ipt[m] <= x)l = m;
		else r = m;
	}
	return l == fr - 1 ? -1 : l;
}
int dp[N_max],right[N_max];

int n, k, d;

int sol(int cur) {
	if (dp[cur] == -1)
		return 0;
	if (cur == n)return 1;
	if (n - cur < k)return 0;
	for (int next =cur+k-1; next < right[cur]; ++next) {
		if (1 == sol(next+1))return 1;
	}
	dp[cur] = -1;
	return 0;
}
int main() {
	scanf("%d %d %d", &n,&k,&d);
	for (int i = 0; i < n; ++i) {
		scanf("%d", ipt+i);
	}
	sort(ipt, ipt + n, cmpv);
	for (int i = 0; i < n; ++i) {
		int l = i, r = n, m;
		while (l + 1<r)
		{
			m = (l + r) / 2;
			if (ipt[m] <= ipt[i] + d)l = m;
			else r = m;
		}
		right[i] = r;
	}
	printf("%s", sol(0)==1?"YES":"NO");
	return 0;
}

猜你喜欢

转载自www.cnblogs.com/tobyw/p/9070155.html