Submission #764651

# Submission time Handle Problem Language Result Execution time Memory
764651 2023-06-23T17:53:55 Z ind1v Watching (JOI13_watching) C++11
100 / 100
111 ms 8236 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 2005;

int n, p, q;
long long a[N];
int dp[N][N / 2];
int xx[N], yy[N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> p >> q;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  if (p + q >= n) {
    cout << 1;
    exit(0);
  }
  sort(a + 1, a + n + 1);
  a[0] = numeric_limits<long long>::min();
  if (q <= p) {
    int l = 1, r = 1000000000, ans = -1;
    while (l <= r) {
      int m = l + r >> 1;
      for (int i = 1, j = 0; i <= n; i++) {
        while (j + 1 < i && a[j + 1] <= a[i] - m) {
          ++j;
        }
        xx[i] = j;
      }
      for (int i = 1, j = 0; i <= n; i++) {
        while (j + 1 < i && a[j + 1] <= a[i] - 2 * m) {
          ++j;
        }
        yy[i] = j;
      }
      memset(dp, 0x3f, sizeof(dp));
      for (int j = 0; j <= q; j++) {
        dp[0][j] = 0;
      }
      for (int i = 1; i <= n; i++) {
        for (int j = 0; j <= q; j++) {
          dp[i][j] = min(dp[i][j], 1 + dp[xx[i]][j]);
          if (j > 0) {
            dp[i][j] = min(dp[i][j], dp[yy[i]][j - 1]);
          }
        }
      }
      if (dp[n][q] <= p) {
        ans = m;
        r = m - 1;
      } else {
        l = m + 1;
      }
    }
    cout << ans;
  } else {
    int l = 1, r = 1000000000, ans = -1;
    while (l <= r) {
      int m = l + r >> 1;
      for (int i = 1, j = 0; i <= n; i++) {
        while (j + 1 < i && a[j + 1] <= a[i] - m) {
          ++j;
        }
        xx[i] = j;
      }
      for (int i = 1, j = 0; i <= n; i++) {
        while (j + 1 < i && a[j + 1] <= a[i] - 2 * m) {
          ++j;
        }
        yy[i] = j;
      }
      memset(dp, 0x3f, sizeof(dp));
      for (int j = 0; j <= p; j++) {
        dp[0][j] = 0;
      }
      for (int i = 1; i <= n; i++) {
        for (int j = 0; j <= p; j++) {
          dp[i][j] = min(dp[i][j], 1 + dp[yy[i]][j]);
          if (j > 0) {
            dp[i][j] = min(dp[i][j], dp[xx[i]][j - 1]);
          }
        }
      }
      if (dp[n][p] <= q) {
        ans = m;
        r = m - 1;
      } else {
        l = m + 1;
      }
    }
    cout << ans;
  }
  return 0;
}

Compilation message

watching.cpp: In function 'int main()':
watching.cpp:28:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   28 |       int m = l + r >> 1;
      |               ~~^~~
watching.cpp:64:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   64 |       int m = l + r >> 1;
      |               ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 8148 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 8 ms 8148 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 8 ms 8196 KB Output is correct
8 Correct 8 ms 8148 KB Output is correct
9 Correct 8 ms 8148 KB Output is correct
10 Correct 7 ms 8148 KB Output is correct
11 Correct 10 ms 8148 KB Output is correct
12 Correct 9 ms 8148 KB Output is correct
13 Correct 10 ms 8188 KB Output is correct
14 Correct 8 ms 8148 KB Output is correct
15 Correct 8 ms 8192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 8212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 11 ms 8148 KB Output is correct
8 Correct 26 ms 8212 KB Output is correct
9 Correct 23 ms 8216 KB Output is correct
10 Correct 20 ms 8204 KB Output is correct
11 Correct 19 ms 8220 KB Output is correct
12 Correct 111 ms 8220 KB Output is correct
13 Correct 10 ms 8236 KB Output is correct
14 Correct 10 ms 8148 KB Output is correct
15 Correct 10 ms 8148 KB Output is correct