[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;
}
### Codeforces Div.2 比赛难介绍 Codeforces Div.2 比赛主要面向的是具有基础编程技能到中级水平的选手。这类比赛通常吸引了大量来自全球不同背景的参赛者,包括大学生、高中生以及一些专业人士。 #### 参加资格 为了参加 Div.2 比赛,选手的评级应不超过 2099 分[^1]。这意味着该级别的竞赛适合那些已经掌握了一定算法知识并能熟练运用至少一种编程语言的人群参与挑战。 #### 题目设置 每场 Div.2 比赛一般会提供五至七道题目,在某些特殊情况下可能会更多或更少。这些题目按照预计解决难递增排列: - **简单题(A, B 类型)**: 主要测试基本的数据结构操作和常见算法的应用能力;例如数组处理、字符串匹配等。 - **中等偏难题(C, D 类型)**: 开始涉及较为复杂的逻辑推理能力和特定领域内的高级技巧;比如图论中的最短路径计算或是动态规划入门应用实例。 - **高难题(E及以上类型)**: 对于这些问题,则更加侧重考察深入理解复杂概念的能力,并能够灵活组合多种方法来解决问题;这往往需要较强的创造力与丰富的实践经验持。 对于新手来说,建议先专注于理解和练习前几类较容易的问题,随着经验积累和技术提升再逐步尝试更高层次的任务。 ```cpp // 示例代码展示如何判断一个数是否为偶数 #include <iostream> using namespace std; bool is_even(int num){ return num % 2 == 0; } int main(){ int number = 4; // 测试数据 if(is_even(number)){ cout << "The given number is even."; }else{ cout << "The given number is odd."; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值