E. Pencils and Boxes
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
inputCopy
6 3 10
7 2 7 7 4 2
outputCopy
YES
inputCopy
6 2 3
4 5 3 13 4 10
outputCopy
YES
inputCopy
3 2 5
10 16 22
outputCopy
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支铅笔,每个铅笔有自己的颜色值,如果两支铅笔的颜色值相差的绝对值<=d,那么可以装进一个背包里,而每个背包要么不装铅笔要么装>=k支铅笔,有无限个可以使用的背包。判断是否有一种装配方案使之满足这些条件,如果存在输出YES,否则输出NO
题解:排序之后,dp[i]=1表示前i支铅笔已经装配完毕(即前i支铅笔已经满足条件地装进背包里),那么dp[i+1]的值为1当且仅当存在一个j(j<=i-k),使dp[j]=1并且a[i]-a[j+1]<=d,很容易想到O(n^2)的写法,显然会T,而由于是寻找区间[1,i-k]内满足dp[j]=1的最大下标j,所以可以借助线段树将复杂度降为O(nlogn)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(x) cout<<#x<<" is "<<x<<endl;
const int maxn=5e5+5;
ll a[maxn];
struct node{
int l;
int r;
int val;
}nod[maxn<<2];
void pushup(int rt){
nod[rt].val=max(nod[(rt<<1)|1].val,nod[rt<<1].val);
}
void build(int rt,int l,int r){
nod[rt].l=l;
nod[rt].r=r;
nod[rt].val=-1;
if(l==r){
return;
}
int mid=(l+r)>>1;
build(rt<<1,l,mid);
build((rt<<1)|1,mid+1,r);
}
void update(int rt,int l,int r,int x){
if(x<l||x>r)return;
if(l==r){
nod[rt].val=l;
return;
}
int mid=(l+r)>>1;
if(mid>=x)update(rt<<1,l,mid,x);
else update((rt<<1)|1,mid+1,r,x);
pushup(rt);
}
int query(int rt,int l,int r,int L,int R){
if(L>R)return -1;
if(l>=L&&r<=R){
return nod[rt].val;
}
int ac=-1;
int mid=(l+r)>>1;
if(mid>=L&&nod[rt<<1].val)
ac=max(ac,query(rt<<1,l,mid,L,R));
if(mid<R&&nod[(rt<<1)|1].val)
ac=max(ac,query((rt<<1)|1,mid+1,r,L,R));
return ac;
}
int main(){
ll n,k,d;
scanf("%lld%lld%lld",&n,&k,&d);
for(int i=2;i<=n+1;i++){scanf("%lld",&a[i]);}
sort(a+2,a+2+n);
build(1,1,n+1);
update(1,1,n+1,1);
int f=0;
for(int i=k+1;i<=n+1;i++){
int w=query(1,1,n+1,1,i-k);
if(w==-1)continue;
if(a[i]-a[w+1]<=d){update(1,1,n+1,i);if(i==n+1)f=1;}
}
if(f)printf("YES\n");
else printf("NO\n");
return 0;
}