Submission #1330940

#TimeUsernameProblemLanguageResultExecution timeMemory
1330940ayazWatching (JOI13_watching)C++20
0 / 100
5 ms9284 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()

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
void run(int tc) {
  int n, p, q;
  cin >> n >> p >> q;
  // n <= 2000, fool!
  p = min(n, p);
  q = min(n, q);
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i++) cin >> a[i];
  sort(a.begin() + 1, a.end());
  long long low = 1, high = 1e9, best = 1e18;
  auto check = [&](long long w) -> bool {
    vector<array<int, 2>> nxt(n + 1);
    for (int i = 1; i <= n; i++) {
      auto it = upper_bound(a.begin() + i, a.end(), a[i] + w - 1);
      nxt[i][0] = (it - 1) - a.begin();
      it = upper_bound(a.begin() + i, a.end(), a[i] + 2 * w - 1);      
      nxt[i][1] = (it - 1) - a.begin();
    }
    debug(nxt);
    vector<vector<int>> dp(p + 1, vector<int>(q + 1, 0));
    for (int small = 0; small <= p; small++) {
      for (int big = 0; big <= q; big++) {
        if (dp[small][big] == -1) continue;

        int idx = dp[small][big];
        if (small < p) dp[small + 1][big] = max({dp[small + 1][big], dp[small][big], nxt[idx + 1][0]});
        if (big < q) dp[small][big + 1] = max({dp[small][big + 1], nxt[idx + 1][1], dp[small][big]});
      }
    }
    debug(dp[p][q]);
    return dp[p][q] >= n;
  };
  while (low <= high) {
    long long 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...