可以证明的是,总是存在一种最优策略使得每个组内的权值都是连续的。

所以排完序一遍 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:

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

Input

The first line contains three integer numbers nk 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 107 2 7 7 4 2

Output
YES

Input
6 2 34 5 3 13 4 10

Output
YES

Input
3 2 510 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;
}

  

转载于:https://www.cnblogs.com/JYYHH/p/9071227.html

CodeForces - 985E Pencils and Boxes相关推荐

  1. CodeForces 985E Pencils and Boxes

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

  2. CodeForces - 985E(Pencils and Boxes)

    题意:分配 n 个铅笔到笔筒中,每个笔筒最少放k个笔,每只铅笔有个value,同一个盒子中放入的笔的value差不能大于d,问能否找到满足条件的方案. 分析:根据题意,我们可以推出答案一定是将铅笔按 ...

  3. Codeforces - 985E Pencils and Boxes

    题意: 给定n支铅笔,问能不能分成若干堆,使得每堆数量不小于k且每堆的最大值和最小值之差不大于d. 题解: 排序.从后往前扫一遍.考虑以每个点为最小值建堆是否合法.对于当前点i,它需要至少k支铅笔,但 ...

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

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

  5. cf 985E Pencils and Boxes

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

  6. Codeforces - Pencils and Boxes

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

  7. CodeForces 985 E Pencils and Boxes

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

  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最近的位置 显然 ...

最新文章

  1. 初级篇第六期:学习UITableView
  2. 计算机怎么设置计算机组和用户,怎样设置同一工作组的计算机资源共享
  3. 计算机任务驱动法教学应用,任务驱动教学法在计算机教学中的应用
  4. Android之PC浏览器上传表单格式大文件到手机客户端read函数阻塞问题
  5. AutoLayout 浅析动画
  6. StackOverFlow优选的十条编程观点
  7. SAP License:第三只眼看财务-现金流量表编制
  8. 如何获取免费比特币?
  9. VALSE学习(四):注意力机制-Attention Network
  10. 20200318_抓取51job招聘数据存数据库
  11. Java 13新特性:switch表达式,文本块
  12. 常见makefile写法
  13. 0/1背包——动态规划
  14. 双重控制门锁程序_智能化发展趋势下,智能门锁如何获得市场青睐?
  15. 《现代控制工程基础》复习笔记
  16. 20110822炒股日记--进入筑底阶段
  17. 华为云桌面远程办公,真香!
  18. 阿拉伯数字对应人民币大写表
  19. 如何让table边框变为单实线?
  20. 人大金仓KingbaseES适配Activiti工作流时踩下的坑

热门文章

  1. 基于STM32F030驱动MQ7一氧化碳传感器
  2. 深圳软件测试 黑盒测试,深圳软件测试培训:常用控件黑盒测试方法有哪些?...
  3. python Selenium浏览器爬虫指南 --未完成版本
  4. BI是报表?BI是可视化?BI到底是什么?
  5. c语言实验程序,C语言实验程序
  6. 常见NoSQL的对比及使用场景(Redis,memcached,mongodb)
  7. RIKIBOT-FX4多线激光雷达用谷歌cartographer构建3D地图
  8. python tkinter数据库通讯录_python连接Mysql数据库写的小电话本
  9. 低级程序员和高级程序员的区别在于?
  10. Greenpois0n绿毒完美越狱教程 ios4.1