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

题目连接:

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 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.

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;
}

原文地址:https://www.cnblogs.com/EDGsheryl/p/9165357.html

时间: 2024-10-15 19:54:06

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes的相关文章

Educational Codeforces Round 44 (Rated for Div. 2)+E. Pencils and Boxes+树状数组

题目链接:E. Pencils and Boxes 题意:N 个数,要求分成任意堆,要求每一堆只要有K个,同一堆中任意数之间差值不能超过d; 题解:用树状数组.排一下序然后从后面开始找,以当前数为最小值看能否成堆,要成堆的话要求i+k,到第一个大于a[i]+d的位置之间有能够成堆的位置.(这里判断的时候树状数组一减看是否大于0就可以了)注意初始化时在n+1的位置加1. 1 #include<bits/stdc++.h> 2 #include <iostream> 3 #includ

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai. You can move these disks

Educational Codeforces Round 71 (Rated for Div. 2) A - There Are Two Types Of Burgers

原文链接:https://www.cnblogs.com/xwl3109377858/p/11404050.html Educational Codeforces Round 71 (Rated for Div. 2) A - There Are Two Types Of Burgers There are two types of burgers in your restaurant — hamburgers and chicken burgers! To assemble a hamburg

Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations

原文链接:https://www.cnblogs.com/xwl3109377858/p/11405773.html Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations You are given a sequence of n pairs of integers: (a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is

Educational Codeforces Round 36 (Rated for Div. 2) 题解

Educational Codeforces Round 36 (Rated for Div. 2) 题目的质量很不错(不看题解做不出来,笑 Codeforces 920C 题意 给定一个\(1\)到\(n\)组成的数组,只可以交换某些相邻的位置,问是否可以将数组调整为升序的 解题思路 首先如果每个数都能通过交换到它应该到的位置,那么就可以调整为升序的. 但实际上交换是对称的,如果应该在的位置在当前位置前方的数都交换完成,那么整体就是排好序的,因为不可能所有不在相应位置的数都在相应位置的后方.

Educational Codeforces Round 55 (Rated for Div. 2)

Educational Codeforces Round 55 (Rated for Div. 2) 链接 A Vasya and Book 傻逼题..注意判边界. #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<set> #include<map> #include<vector> #include<cm

Educational Codeforces Round 57 (Rated for Div. 2)

get人生第二场CF! 成绩:(exACM) rank858 AC3/7 Penalty57 rating1648(+52) 题目:Educational Codeforces Round 57 (Rated for Div. 2) 错题题解: D. Easy Problem E. The Top Scorer F. Inversion Expectation G. Lucky Tickets 原文地址:https://www.cnblogs.com/xht37/p/10198321.html

Educational Codeforces Round 58 (Rated for Div. 2)(待更新)

get人生第七场CF! 成绩:(exACM) rank AC3/7 Penalty104 rating() 题目:Educational Codeforces Round 58 (Rated for Div. 2) 错题题解: C. Division and Union 原文地址:https://www.cnblogs.com/xht37/p/10260260.html