제출 #1330909

#제출 시각아이디문제언어결과실행 시간메모리
1330909ayaz구경하기 (JOI13_watching)C++20
0 / 100
1 ms344 KiB
// Finally WSL
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

#define all(x) (x).begin(), (x).end()
#define isz(x) (int)x.size()
#define int long long

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
void run(int tc) {
  int n, p, q;
  cin >> n >> p >> q;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  sort(a.begin() + 1, a.end());
  int low = 1, high = a[n] - a[1] + 1, best = a[n] - a[1] + 1;
  auto check = [&](int w) -> bool {
    vector<array<int, 2>> group;
    for (int i = 1; i <= n;) {
      auto it = upper_bound(a.begin() + i, a.end(), a[i] + w - 1);
      if (it != a.end()) {
        debug(w, a[i], *(it - 1));
        group.push_back({a[i], *(it - 1)});
        i = it - a.begin();
      } else {
        debug(w, a[i], a[n]);
        group.push_back({a[i], a[n]});
        i = n + 1;
      }
    }
    int cntSmall = group.size(), cntBig = 0;
    if (cntSmall <= p)
      return true;
    assert(1 <= cntSmall && cntSmall <= n);
    for (int i = 0; i < group.size() - 1; i++) {
      auto [l1, r1] = group[i];
      auto [l2, r2] = group[i + 1];
      assert(r1 <= l2);
      if (cntBig + 1 <= q && cntSmall - 2 >= 0 && (r2 - l1 + 1) <= 2 * w) {
        cntBig++;
        cntSmall -= 2;
        i++;
      }
      if (cntBig <= q && cntSmall <= p) return true;
    }
    return false;
  };
  while (low <= high) {
    int mid = (low + high) >> 1;
    if (check(mid)) {
      high = mid - 1;
      best = mid;
    } else {
      low = mid + 1;
    }
  }
  cout << best << endl;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int t = 1;
  for (int tc = 1; tc <= t; tc++) run(tc);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...