cf 985E Pencils and Boxes

一 原题

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, ..., 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 nk and d (1 ≤ k ≤ n ≤ 5·1050 ≤ 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
Copy
6 3 10
7 2 7 7 4 2
output
Copy
YES
input
Copy
6 2 3
4 5 3 13 4 10
output
Copy
YES
input
Copy
3 2 5
10 16 22
output
Copy
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个数,问你可以可以把它们分成若干组,一个组内至少有k个数,并且组内的数两两之间差的绝对值不超过d。n<=5*10^5。

先排序,接着动态规划解决:dp[i]表示前i个数能否被划分,判断dp[i+1]时,只要找到 j 满足:1)i-j>k-2,2)dp[j]为true,3)a[i+1]<=a[j+1]+d就可以了。暴力来找这样的j是O(n^2)的,假设数组a中a[i+1]-d的lower_bound是a[m],我们只关心a[m-1]~a[i-k+1]之间有没有dp值为true的下标,利用树状数组统计前缀和可以把复杂度降到O(n*lgn)


三 代码

#include <cstdio>
#include <cstring>
#include <algorithm>

const int maxn = 5e5 + 5;

struct BinaryIndexedTree {
    int up;
    long long tree[maxn];
    
    void init(int u) {
        up = u;
        memset(tree, 0, sizeof(tree));
    }
    
    inline int lowbit(int x) { return (x & (-x)); }
    
    void update(int pos, long long val) {
        while (pos <= up) { tree[pos] += val; pos += lowbit(pos); } 
    }
    
    long long calc(int pos) {
        long long ret = 0;
        while (pos) { ret += tree[pos]; pos -= lowbit(pos); }
        return ret;
    }
} biTree;  // init before any further operations, index starts from 1

int n, k, d, a[maxn];
bool dp[maxn];

int main() {
    scanf("%d%d%d", &n, &k, &d);
    biTree.init(n);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    std::sort(a + 1, a + n + 1);
    for (int i = 1; i <= n; i++) {
        if (i < k) dp[i] = false;
        else if (a[i] - a[1] <= d) dp[i] = true;
        else {
            int t = std::lower_bound(a + 1, a + n + 1, a[i] - d) - a - 1;
            if (t > i - k) dp[i] = false;
            else dp[i] = (biTree.calc(i - k) - biTree.calc(t - 1) > 0);
        }
        if (dp[i]) biTree.update(i, 1LL);
    }
    if (dp[n]) puts("YES");
    else puts("NO");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值