[CF] E. Pencils and Boxes - Educational Codeforces Round 44 (Rated for Div. 2)

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

题目大意

给出 $n \ (\le 5 \times 10^5)$ 根铅笔,要求放入 $k \ (1 \le k \le n)$ 个盒子。每只铅笔有一个属性 $a_i \ (1 \le a_i \le 10^9)$。

要求盒子要么为空,要么有至少 $k$ 只铅笔,且在同一盒子中的任意一对 $a_i, a_j$ 满足 $|a_i - a_j| \le d$ 其中 $0 \le d \le 10^9$。

问是否存在这样的分配方案。

简要题解

观察:如果存在分配方案,则总可以分成按照长度排序的一些连续段。

实际上这个观察至关重要,但其实第一次做题的时候并没有证明这件事。这里我们某答案不是按照上述观察分段的,则假定 $l$ 最小的一段是 $i$,如果我们找不到任何与 $[l_i, r_i]$ 相交的段,则说明其符合上述规律。否则必有与其相交的 $j$。

此时 $l_i < l_j$ 若 $l_j < r_i < r_j$ 显然我们可以尽可能的将 $i$ 尾部选取的元素和 $j$ 头部元素交换,而不破坏和 $d$ 的关系。每次这样的交换 $l_j$ 会增大,$r_i$ 会变小,因此最终会使 $i, j$ 不相交。

i [        ]
j       [        ]

若 $l_j < r_j < r_i$,显然我们对两组排序,再分按大小和原先的成数量,分成两组,也不会破坏 $d$ 的限制。

i [               ]
j    [        ]

两种情况我们都没有改变每段的元素数量,因此 $k$ 的限制也都满足。至此,我们依次处理所有和 $i$ 相交的段,于是最后我们得到了一段最小的且不与其他段相交的 $i$。不断重复这个过程,最终所有段都不相交。

有了这个观察之后,我们就可以写动态规划了。$a$ 排序后, $dp[i]$ 表示到 $i$ 为止 $1 \sim i$ 是否有合法划分。于是我们可以枚举最后一段选了哪些,这段要有至少 $k$ 长,且最小元素 $a[j] + d >= a[i]$,记这个位置为 $l$ 则。注意到这是连续的一段,因此前缀和处理一下这一段是否有合法的转移即可。

复杂度

$T$:$O(n \log n)$

$S$:$O(n)$

代码实现

#include <bits/stdc++.h>
using namespace std;

int io_=[](){ ios::sync_with_stdio(false); cin.tie(nullptr); return 0; }();

using LL = long long;
using ULL = unsigned long long;
using LD = long double;
using PII = pair<int, int>;
using VI = vector<int>;
using MII = map<int, int>;

template<typename T> void cmin(T &x,const T &y) { if(y<x) x=y; }
template<typename T> void cmax(T &x,const T &y) { if(x<y) x=y; }
template<typename T> bool ckmin(T &x,const T &y) { 
    return y<x ? (x=y, true) : false; }
template<typename T> bool ckmax(T &x,const T &y) { 
    return x<y ? (x=y, true) : false; }
template<typename T> void cmin(T &x,T &y,const T &z) {// x<=y<=z 
    if(z<x) { y=x; x=z; } else if(z<y) y=z; }
template<typename T> void cmax(T &x,T &y,const T &z) {// x>=y>=z
    if(x<z) { y=x; x=z; } else if(y<z) y=z; }

// mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
// mt19937_64 rnd_64(chrono::system_clock::now().time_since_epoch().count());

/*
---------1---------2---------3---------4---------5---------6---------7---------
1234567890123456789012345678901234567890123456789012345678901234567890123456789
*/

void solve() {
  int n, k, d; cin >> n >> k >> d;
  vector<int> a(n);
  for (int& i : a) cin >> i;
  
  sort(a.begin(), a.end());

  vector<int> dp(n + 1); // sum of dp;
  dp[0] = 1;

  for (int i = 1; i <= n; i++) {
    dp[i] = dp[i - 1];

    int l = lower_bound(a.begin(), a.end(), a[i - 1] - d) - a.begin();
    int r = i - k;

    // cerr << a[i - 1] << ' ' << l << ' ' << r << endl;

    if (l <= r) {
      int sum = dp[r];
      if (l) sum -= dp[l - 1];

      if (sum) dp[i]++;
    }
  }

  cout << ((dp[n] - dp[n - 1]) ? "YES" : "NO") << '\n';
}

int main() {
  int t = 1; 
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
Prev: [CF] C. Yet Another Card Deck - Educational Codeforces Round 107 (Rated for Div. 2)
Next: [CF] D. Sand Fortress - Educational Codeforces Round 44 (Rated for Div. 2)