CodeForces - 985E Pencils and Boxes

    能夠證實的是,老是存在一種最優策略使得每一個組內的權值都是連續的。c++

    因此排完序一遍 two pointers就好啦。git

 

Discriptionchrome

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 ninteger 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:this

  • 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 iand 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".spa

Inputcode

The first line contains three integer numbers nk and d (1 ≤ k ≤ n ≤ 5·1050 ≤ 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.blog

The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 109) — saturation of color of each pencil.three

Outputip

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

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 3pencils 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.

 

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=500005;
int n,a[maxn],K,D,now,L,R;
bool can[maxn];

inline int read(){
	int x=0; char ch=getchar();
	for(;!isdigit(ch);ch=getchar());
	for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
	return x;
}

inline void solve(){
	L=R=0,can[0]=now=1;
	for(int i=1;i<K;i++) can[i]=0;
	for(int i=K;i<=n;i++){
		while(a[i]-a[L+1]>D){ now-=(int)can[L],L++;}
		while(R<i-K){ R++,now+=(int)can[R];}
//		printf("%d %d %d %d\n",i,L,R,now);
		can[i]=now>0?1:0;
	}
}

int main(){
	n=read(),K=read(),D=read();
	for(int i=1;i<=n;i++) a[i]=read();
	sort(a+1,a+n+1);
	
	solve();
	
	puts(can[n]?"YES":"NO");
	return 0;
}
相關文章
相關標籤/搜索