Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes[线段树+dp]

探讨了一种将具有不同饱和度的铅笔分配到多个盒子中的算法问题,目标是在满足特定条件的情况下,确保每个盒子至少包含k支铅笔,且任意两支铅笔的颜色值差不超过d。通过排序和动态规划结合线段树优化,实现了O(nlogn)的解决方案。

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

E. Pencils and Boxes
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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, …, 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
inputCopy
6 3 10
7 2 7 7 4 2
outputCopy
YES
inputCopy
6 2 3
4 5 3 13 4 10
outputCopy
YES
inputCopy
3 2 5
10 16 22
outputCopy
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支铅笔,每个铅笔有自己的颜色值,如果两支铅笔的颜色值相差的绝对值<=d,那么可以装进一个背包里,而每个背包要么不装铅笔要么装>=k支铅笔,有无限个可以使用的背包。判断是否有一种装配方案使之满足这些条件,如果存在输出YES,否则输出NO

题解:排序之后,dp[i]=1表示前i支铅笔已经装配完毕(即前i支铅笔已经满足条件地装进背包里),那么dp[i+1]的值为1当且仅当存在一个j(j<=i-k),使dp[j]=1并且a[i]-a[j+1]<=d,很容易想到O(n^2)的写法,显然会T,而由于是寻找区间[1,i-k]内满足dp[j]=1的最大下标j,所以可以借助线段树将复杂度降为O(nlogn)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(x) cout<<#x<<" is "<<x<<endl;
const int maxn=5e5+5;
ll a[maxn];
struct node{
    int l;
    int r;
    int val;
}nod[maxn<<2];
void pushup(int rt){
    nod[rt].val=max(nod[(rt<<1)|1].val,nod[rt<<1].val);
}
void build(int rt,int l,int r){
    nod[rt].l=l;
    nod[rt].r=r;
    nod[rt].val=-1;
    if(l==r){
        return;
    }
    int mid=(l+r)>>1;
    build(rt<<1,l,mid);
    build((rt<<1)|1,mid+1,r);
}
void update(int rt,int l,int r,int x){
    if(x<l||x>r)return;
    if(l==r){
        nod[rt].val=l;
        return;
    }
    int mid=(l+r)>>1;
    if(mid>=x)update(rt<<1,l,mid,x);
    else update((rt<<1)|1,mid+1,r,x);
    pushup(rt);
}
int query(int rt,int l,int r,int L,int R){
    if(L>R)return -1;
    if(l>=L&&r<=R){
        return nod[rt].val;
    }
    int ac=-1;
    int mid=(l+r)>>1;
    if(mid>=L&&nod[rt<<1].val)
        ac=max(ac,query(rt<<1,l,mid,L,R));
    if(mid<R&&nod[(rt<<1)|1].val)
        ac=max(ac,query((rt<<1)|1,mid+1,r,L,R));
    return ac;
}
int main(){
    ll n,k,d;
    scanf("%lld%lld%lld",&n,&k,&d);
    for(int i=2;i<=n+1;i++){scanf("%lld",&a[i]);}
    sort(a+2,a+2+n);
    build(1,1,n+1);
    update(1,1,n+1,1);
    int f=0;
    for(int i=k+1;i<=n+1;i++){
        int w=query(1,1,n+1,1,i-k);
        if(w==-1)continue;
        if(a[i]-a[w+1]<=d){update(1,1,n+1,i);if(i==n+1)f=1;}
    }
    if(f)printf("YES\n");
    else printf("NO\n");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值