1. 程式人生 > >Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

problem 滿足 names fort determine resp binary nat 題意

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

題目連接:

http://codeforces.com/contest/985/problem/E

Description

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 a

1,?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.

Sample Input

6 3 10
7 2 7 7 4 2

Sample Output

YES

題意

有n個數字,將他們分成若幹組,每組至少k個元素,組內元素差不超過d.

There are n numbers, devide them into many groups, on which there are at least k elements.
If x,y in the same groups, they fits \(|x-y|<d\).

題解:

首先將元素按大小排序。dp[i]表示前i個可以滿足條件。轉移方程: dp[i] = dp[j]=1?1:0; 其中\(j<i-k\)\(a[i]-a[j]<=d\). 我們用樹狀數組維護區間,用二分查找去找最小的j。

sort the elements for the value. dp[i] represent former i elements can fit the condition. The transform equation is: dp[i] = dp[j]=1?1:0; (\(j<i-k\)\(a[i]-a[j]<=d\)). We use BIT to maintain the segment, and use binary search to find the minimum j.

代碼

#include <bits/stdc++.h>

using namespace std;

int n, k, d;
int a[500010];
int dp[500010];
int c[500010];

void update(int k) {
    while (k <= n) {
        c[k]++;
        k |= k + 1;
    }
};

int sum(int k) {
    int ret = 0;
    while (k >= 0) {
        ret += c[k];
        k = ((k + 1) & k) - 1;
    }
    return ret;
}

int get(int l, int r) {
    if (l > r) return 0;
    return sum(r) - sum(l - 1);
}

int main() {
    cin >> n >> k >> d;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    sort(a + 1, a + n + 1, less<int>());
    int l = 0;
    dp[0] = 1;
    update(0);
    for (int i = 1; i <= n; i++) {
        while (l < i && a[i] - a[l] > d) l++;
        dp[i] = get(l - 1, i - k) >= 1 ? 1 : 0;
        if (dp[i]) update(i);
    }
    cout << (dp[n] ? "YES" : "NO") << endl;
}

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes