Advertisement
tuki2501

JOI13_watching.cpp

Jan 31st, 2022
630
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 2005;
  5.  
  6. int main() {
  7.   cin.tie(0)->sync_with_stdio(0);
  8.   int n, p, q;
  9.   cin >> n >> p >> q;
  10.   vector<int> a(n + 1);
  11.   for (int i = 1; i <= n; i++) {
  12.     cin >> a[i];
  13.   }
  14.   if (n - p - q <= 0) {
  15.     cout << 1 << '\n';
  16.     return 0;
  17.   }
  18.   sort(a.begin() + 1, a.end());
  19.   auto check = [&](int m) {
  20.     vector<vector<int>> dp(n + 1, vector<int>(p + 1, 1e9));
  21.     for (int i = 0; i <= p; i++) {
  22.       dp[0][i] = 0;
  23.     }
  24.     int pp = 1, pq = 1;
  25.     for (int i = 1; i <= n; i++) {
  26.       while (a[i] - a[pp] + 1 >     m) pp++;
  27.       while (a[i] - a[pq] + 1 > 2 * m) pq++;
  28.       dp[i][0] = dp[pq - 1][0] + 1;
  29.       for (int j = 1; j <= p; j++) {
  30.         dp[i][j] = min(dp[pq - 1][j] + 1, dp[pp - 1][j - 1]);
  31.       }
  32.     }
  33.     return (dp[n][p] <= q);
  34.   };
  35.   int l = 1, r = 1e9, ans = -1;
  36.   while (l <= r) {
  37.     int m = (l + r) / 2;
  38.     if (check(m)) {
  39.       ans = m;
  40.       r = m - 1;
  41.     }
  42.     else l = m + 1;
  43.   }
  44.   cout << ans << '\n';
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement