CodeForces 985E Pencils and Boxes

解决Mishka如何将多色铅笔按特定条件分配到盒子中的算法问题,确保每个盒子至少有k支铅笔,且同一盒子内的铅笔颜色饱和度差不超过d。

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

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, a_2, ..., a_n\) 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 \(|a_i - a_j| \le d\), where \(|x|\) means absolute value of \(x\). Note that the opposite is optional, there can be pencils \(i\) and \(j\) such that \(|a_i - a_j| \le 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 \le k \le n \le  5\cdot10^5, 0 \le d \le 10^9\)) — 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 \(a_1, a_2, \dots, a_n (1 \le a_i \le 10^9)\) — 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

6 3 10
7 2 7 7 4 2

output

YES

input

6 2 3
4 5 3 13 4 10

output

YES

input

3 2 5
10 16 22

output

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.

Solution

题意:给\(n\)个数,同时给定\(k\)\(d\),问能否将这\(n\)个数分成若干个集合,每个集合大小至少为\(k\),且同一个集合中的任何两个元素之差不超过\(d\)

同一个集合中的任何两个元素之差不超过\(d\)这一条件相当于集合中最大最小元素之差不超过\(d\),所以很自然地想到要先对数组\(a\)排序,然后用\(f[i]\)表示前\(i\)个元素能否合法地划分,\(a[i]\)所在的集合对应着\(a_{1}, a_{2}, \dots, a_{i}\)的一个后缀,可以通过二分找到这一后缀的所有可能开始的位置,只要有一个可能的开始位置\(p\)满足\(f[p-1] = 1\),则\(f[i] = 1\),否则\(f[i] = 0\),复杂度\(O(n \log n)\)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 500011;
int a[maxn], f[maxn], sum[maxn];
int main() {
  int n, k, d;
  scanf("%d%d%d", &n, &k, &d);
  for (int i = 1; i <= n; ++i)  scanf("%d", a + i);
  sort(a + 1, a + n + 1);
  sum[0] = 1;
  for (int i = 1; i <= n; ++i) {
    int p = lower_bound(a + 1, a + 1 + n, a[i] - d) - a;
    int p1 = p - 1, p2 = i - k; // [p1, p2]是可能的开始位置
    if (p2 >= p1) {
      f[i] = sum[p2] - (p1 > 0 ? sum[p1 - 1] : 0) > 0;
    }
    sum[i] = sum[i - 1] + f[i];
  }
  puts(f[n] ? "YES" : "NO");
  return 0;
}

转载于:https://www.cnblogs.com/hitgxz/p/9977657.html

### Codeforces 887E Problem Solution and Discussion The problem **887E - The Great Game** on Codeforces involves a strategic game between two players who take turns to perform operations under specific rules. To tackle this challenge effectively, understanding both dynamic programming (DP) techniques and bitwise manipulation is crucial. #### Dynamic Programming Approach One effective method to approach this problem utilizes DP with memoization. By defining `dp[i][j]` as the optimal result when starting from state `(i,j)` where `i` represents current position and `j` indicates some status flag related to previous moves: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = ...; // Define based on constraints int dp[MAXN][2]; // Function to calculate minimum steps using top-down DP int minSteps(int pos, bool prevMoveType) { if (pos >= N) return 0; if (dp[pos][prevMoveType] != -1) return dp[pos][prevMoveType]; int res = INT_MAX; // Try all possible next positions and update 'res' for (...) { /* Logic here */ } dp[pos][prevMoveType] = res; return res; } ``` This code snippet outlines how one might structure a solution involving recursive calls combined with caching results through an array named `dp`. #### Bitwise Operations Insight Another critical aspect lies within efficiently handling large integers via bitwise operators instead of arithmetic ones whenever applicable. This optimization can significantly reduce computation time especially given tight limits often found in competitive coding challenges like those hosted by platforms such as Codeforces[^1]. For detailed discussions about similar problems or more insights into solving strategies specifically tailored towards contest preparation, visiting forums dedicated to algorithmic contests would be beneficial. Websites associated directly with Codeforces offer rich resources including editorials written after each round which provide comprehensive explanations alongside alternative approaches taken by successful contestants during live events. --related questions-- 1. What are common pitfalls encountered while implementing dynamic programming solutions? 2. How does bit manipulation improve performance in algorithms dealing with integer values? 3. Can you recommend any online communities focused on discussing competitive programming tactics? 4. Are there particular patterns that frequently appear across different levels of difficulty within Codeforces contests?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值