Description

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 \(a_1, a_2, ..., a_n\) 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 \(|a_i - a_j| \le d\), where \(|x|\) means absolute value of \(x\). Note that the opposite is optional, there can be pencils \(i\) and \(j\) such that \(|a_i - a_j| \le 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 \le k \le n \le  5\cdot10^5, 0 \le d \le 10^9\)) — 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 \(a_1, a_2, \dots, a_n (1 \le a_i \le 10^9)\) — 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.

Solution

题意:给\(n\)个数,同时给定\(k\)和\(d\),问能否将这\(n\)个数分成若干个集合,每个集合大小至少为\(k\),且同一个集合中的任何两个元素之差不超过\(d\)

同一个集合中的任何两个元素之差不超过\(d\)这一条件相当于集合中最大最小元素之差不超过\(d\),所以很自然地想到要先对数组\(a\)排序,然后用\(f[i]\)表示前\(i\)个元素能否合法地划分,\(a[i]\)所在的集合对应着\(a_{1}, a_{2}, \dots, a_{i}\)的一个后缀,可以通过二分找到这一后缀的所有可能开始的位置,只要有一个可能的开始位置\(p\)满足\(f[p-1] = 1\),则\(f[i] = 1\),否则\(f[i] = 0\),复杂度\(O(n \log n)\)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 500011;
int a[maxn], f[maxn], sum[maxn];
int main() {int n, k, d;scanf("%d%d%d", &n, &k, &d);for (int i = 1; i <= n; ++i)  scanf("%d", a + i);sort(a + 1, a + n + 1);sum[0] = 1;for (int i = 1; i <= n; ++i) {int p = lower_bound(a + 1, a + 1 + n, a[i] - d) - a;int p1 = p - 1, p2 = i - k; // [p1, p2]是可能的开始位置if (p2 >= p1) {f[i] = sum[p2] - (p1 > 0 ? sum[p1 - 1] : 0) > 0;}sum[i] = sum[i - 1] + f[i];}puts(f[n] ? "YES" : "NO");return 0;
}

转载于:https://www.cnblogs.com/hitgxz/p/9977657.html

CodeForces 985E Pencils and Boxes相关推荐

  1. CodeForces - 985E Pencils and Boxes

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

  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. 双向非循环递增链表——插入,删除,清空
  2. SMB文件共享及用户权限使用配置
  3. jmeter执行python脚本,Jmeter+Python-1问题记录jmeter执行Python3的脚本时报 ValueErro
  4. SpringCloud:Zuul 路由访问(基本使用、路由功能、过滤访问、服务降级)
  5. 2011年5月19日盘后分析:把握行情运行主线 静待大盘明确信号
  6. IT 行业的创新 - 创新的迷思 (1-4)
  7. MNTD论文修改20211114(Y Zhou)
  8. 7-211 求前缀表达式的值 (25 分)
  9. XBOX Series X规格如此强悍,如果被破解安装了win10,将对PC行业带来什么影响?
  10. Sql语句常用关键字
  11. Java中实现十进制数转换为二进制的三种方法
  12. Python——彩票(大乐透)模拟随机选号
  13. 【CLP】Conic Linear Programming Duality
  14. UGUI源代码之Image-Sliced模式
  15. HTML5 实现小车动画效果(Canvas/CSS3/JQuery)
  16. MySQL 排序语句
  17. VLookup函数详细教程
  18. java,png,jpg,多张图片合成一个pdf,压缩图片,并且保证图片不失帧。
  19. AI-039: Python深度学习3 - 三个Karas实例-1
  20. UTF-8与UTF-8(BOM)区别和一些说明

热门文章

  1. C++ 程序 Crash 的分析
  2. 基于视频的车辆识别技术
  3. Eureka健康机制检查问题之一创建EurekaDiscoveryClientConfiguration$EurekaHealthCheckHandlerConfiguration错误
  4. 企业应用大数据的三重境界:数据·分析·成果
  5. zip不是内部或外部命令,也不是可执行程序”详细解决办法
  6. 组态王与mysql数据库通过ODBC连接
  7. CCF CSP 压缩编码
  8. 自我设限的跳蚤效应(Flea Effect)
  9. OSChina 周一乱弹 ——七夕把室友变成妩媚爱人
  10. Python经典问题——身体指数BMI