[Codeforces 985.E] Pencils and Boxes(dp,前缀和优化)

题目要求将铅笔分配到盒子里,每个盒子至少有k支铅笔,并且同一盒内的铅笔颜色饱和度差不超过d。解题思路是通过排序和动态规划来判断是否可能满足条件。dp[i]表示前i个铅笔是否能放入盒子,通过前缀和优化,可以在O(N)的时间复杂度内完成查询。

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

E. Pencils and Boxes

time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

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,...,ana1, 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 kk pencils in it;
  • If pencils i and jj belong to the same box, then |aiaj|d, where |x||x| means absolute value of xx. Note that the opposite is optional, there can be pencils i and jj such that |aiaj|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 nn, k and dd (1kn5·105, 0d1090 ≤ 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,...,ana1, a2, ..., an (1ai1091 ≤ 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

InputOutput
6 3 10
7 2 7 7 4 2
YES
6 2 3
4 5 3 13 4 10
YES
3 2 5
10 16 22
NO

Note

In the first example it is possible to distribute pencils into 22 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 1010.

In the second example you can split pencils of saturations [4,5,3,4][4, 5, 3, 4] into 22 boxes of size 2 and put the remaining ones into another box.


解题思路

将数列排序,若有解,则一定存在一种数列划分的方式(即把连续一段铅笔放进一个盒子中)满足条件。
dp[i]dp[i] 表示前 ii 个铅笔能否放进盒子中(1能,00不能),那么转移非常简单:只要存在一个 dp[j]=1,那么 dp[i]=1dp[i]=1,其中 jj 满足 1jikaiaj+1dai−aj+1⩽d(仔细想想为什么是 aj+1aj+1
可是如果遍历 jj 的话复杂度是 O(N2) 的,显然需要优化——其实我们只需要知道 jj 所在的那个区间内是否有 1 即可,或者说,和大于 00 即可。所以我们可以用前缀和轻松做到 O(1) 查询。

综上:

  • dp状态:dp[i]dp[i] 表示前 ii 个铅笔能否放进盒子中(1能,00不能)
  • dp方程:dp[i]=(sum(pos1,ik)>0),其中 pospos 是最小的满足 a[i]a[pos]da[i]−a[pos]⩽d 的点
  • dp顺序:有dp方程可得:从小到大枚举 ii 即可
  • 边界条件:这道题的边界条件有点迷……在实践过程中你会发现那些从 1 开始的符合题意的区间会被判 00,所以边界条件可以是把它们置为 1。其他合法区间都可以从它们转移过去。

时间复杂度 O(N)O(N)


Code

#include<cstdio>
#include<algorithm>

using namespace std;

const int N = 500005;
int n, k, d, a[N], ptl, ptr, cnt, dpSum[N];
bool dp[N]; // dp[i] represents whether we can put 1st~ith pencils into boxes

inline int sum(int l, int r){
    if(l > r)   return 0;
    return dpSum[r] - dpSum[l-1];
}

int main(){
    scanf("%d%d%d", &n, &k, &d);
    for(int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    sort(a+1, a+n+1);
    for(int i = k; i <= n; i++)
        if(a[i] - a[1] <= d)
            dp[i] = 1;
    int pos = 1;
    for(int i = 1; i <= n; i++){
        while(a[i] - a[pos] > d)    pos++;
        dp[i] |= sum(pos-1, i - k);
        dpSum[i] = dpSum[i-1] + dp[i];
    }
    return puts(dp[n] ? "YES" : "NO"), 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值