原题链接:http://codeforces.com/problemset/problem/985/E

Pencils and Boxes

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.

题目大意

给定nnn个数,要放到多个盒子里,要求每个盒子至少有k" role="presentation" style="position: relative;">kkk个数,盒子中数的最大值与最小值之差小于ddd,问是否有合法划分方案。

题解

比较显然的是,排序之后,将连续的一段数字放在一起肯定较优。假设排完序后的序列为x" role="presentation" style="position: relative;">xxx,dp[i]dp[i]dp[i]表示前iii个数能否被划分。对于xi" role="presentation" style="position: relative;">xixix_i,它可以跟[xi−d→xi−k+1,xi−1][xi−d→xi−k+1,xi−1][x_i-d\to x_{i-k+1},x_{i-1}]中的任意一个区间的数放在一起,我们只需要查找[xi−d,xi−k+1][xi−d,xi−k+1][x_i-d,x_{i-k+1}]这段区间内有没有dpdpdp值为11<script type="math/tex" id="MathJax-Element-12">1</script>的就行了。

代码
#include<bits/stdc++.h>
using namespace std;
const int M=5e5+5;
int n,k,d,le,a[M],dp[M],has[M];
void in(){scanf("%d%d%d",&n,&k,&d);for(int i=1;i<=n;++i)scanf("%d",&a[i]);}
void ac()
{sort(a+1,a+1+n);dp[0]=has[0]=1;for(int i=1;i<=n;++i){while(le<=i-k&&a[i]-a[le+1]>d)++le;if(i>=k&&le+k<=i&&has[i-k]-has[le-1]>=1)dp[i]=1;has[i]=has[i-1]+dp[i];}puts(dp[n]?"YES":"NO");
}
int main(){in();ac();}

CF985E Pencils and Boxes相关推荐

  1. CF985E Pencils and Boxes(树状数组+dp)

    树状数组优化dp问题 #include<bits/stdc++.h> using namespace std; const int N=5e5+10; int n,k,d; int b[N ...

  2. Codeforces - Pencils and Boxes

    题目链接:Codeforces - Pencils and Boxes 显然可以dp,dp[i] 为前 i 个是否合法. 然后sort一下,然后枚举当前位置的时候,二分或者尺取找到最远的合法的位置.然 ...

  3. cf 985E Pencils and Boxes

    一 原题 E. Pencils and Boxes time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  4. CodeForces 985 E Pencils and Boxes

    Pencils and Boxes 题意: n支铅笔,每只笔有一个颜色值, 无限个盒子, 如果一个盒子里面放笔的话, 就至少需要k支笔装在一起,并且一个盒子里面的笔的颜色值只差 不能大于 d.如果满足 ...

  5. CodeForces 985E Pencils and Boxes

    Description Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives ...

  6. codeforces contest 985E. Pencils and Boxes+思维

    类似指针的想法 E. Pencils and Boxes time limit per test 2 seconds memory limit per test 256 megabytes input ...

  7. CodeForces - 985E Pencils and Boxes

    可以证明的是,总是存在一种最优策略使得每个组内的权值都是连续的. 所以排完序一遍 two pointers就好啦. Discription Mishka received a gift of mult ...

  8. E. Pencils and Boxes codeforces(思维+dp)

    题目链接:E. Pencils and Boxes 题意: 给出n个数字,分组,每组大小>=k,并且同一组内任意两个数字大小不能超过d 思路: 先从小到大排序.然后dp.起点肯定是第一个数字,然 ...

  9. Codeforces 985 E - Pencils and Boxes

    E - Pencils and Boxes 思路: dp 先排个序,放进一个袋子里的显然是一段区间 定义状态:pos[i]表示小于等于i的可以作为(放进一个袋子里的)一段区间起点的离i最近的位置 显然 ...

  10. E. Pencils and Boxes(尺取dp)

    E. Pencils and Boxes(尺取&dp) 显然按val排序,然后考虑dp,显然转移的jjj随iii增加不会递减,接着考虑限制条件,显然i往右走,要是差值小于d,i递增, 因为有k ...

最新文章

  1. 语言模型如何为大象“称”体重?斯坦福提出“尺度探测”新思路
  2. 95后热搜哪些事,夸克用AI引擎发布2021年度关键词
  3. WinCE中的Debug Zone调试
  4. Jekyll + Coding Pages 搭建静态博客
  5. Android 属性动画实现一个简单的PopupWindow
  6. MySQL 数据库操作命令汇总
  7. 3.自编码器(变分自编码器,VAE)
  8. 图解对比MySQL索引为什么要用B+树
  9. python入门经典100题
  10. TrueCrypt加密安全问题
  11. ubuntu等linux系统如何阅读caj文档
  12. python实战:基于链家网二手房数据解析任务
  13. 世界卫生组织国际癌症研究机构致癌物清单
  14. 【Ceph】Ceph Client
  15. 一个简单的个人视频点播网站制作(一)
  16. nginx配置https双向验证(ca机构证书+自签证书)
  17. 计算机和机械课程有关联吗,机械工程及自动化有哪些课程
  18. hadoop下载包目录结构
  19. python程序设计从基础到开发课后题答案夏敏捷_[转载] python程序设计应用教程夏敏捷答案第八章_Python程序设计:从基础到开发...
  20. 蚂蚁金服高级前端专家,我做前端这 10 多年来的感悟

热门文章

  1. 向量组A可以由一个向量组B表出,并且A的秩小于B的秩,那么A线性相关
  2. Websocket 从header读取数据
  3. Python——Pycharm基本设置
  4. Android NavigationView中设置menu中的item字体颜色
  5. Openstack Nova network
  6. java Monitor对象监视器、对象头、mark word
  7. 并发编程学习之线程池
  8. .NET中各种数据库连接大全
  9. android ffmpeg4.0.2编译过程记录
  10. 在CAD中容易混淆的概念