CodeForces - 985E Pencils and Boxes
? ? 可以证明的是,总是存在一种最优策略使得每个组内的权值都是连续的。 ? ? 所以排完序一遍 two pointers就好啦。 ? Discription 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:
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,?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
Output
YES
Input
6 2 3
Output
YES
Input
3 2 5
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 %dn",i,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; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |