CF985E Pencils and Boxes

本文解析了CodeForces上一道关于将彩色铅笔按特定条件分组的问题。通过动态规划的方法,对输入数据进行排序并确定每组铅笔是否满足颜色差异限制,最终判断是否能完成分组。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题链接: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.

题目大意

给定 n n 个数,要放到多个盒子里,要求每个盒子至少有k个数,盒子中数的最大值与最小值之差小于 d d ,问是否有合法划分方案。

题解

比较显然的是,排序之后,将连续的一段数字放在一起肯定较优。假设排完序后的序列为x dp[i] d p [ i ] 表示前 i i 个数能否被划分。对于xi,它可以跟 [xidxik+1,xi1] [ x i − d → x i − k + 1 , x i − 1 ] 中的任意一个区间的数放在一起,我们只需要查找 [xid,xik+1] [ x i − d , x i − k + 1 ] 这段区间内有没有 dp d p 值为 1 1 的就行了。

代码
#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();}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShadyPi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值