Submission #958905

# Submission time Handle Problem Language Result Execution time Memory
958905 2024-04-07T03:33:07 Z ad_red Watching (JOI13_watching) C++17
100 / 100
530 ms 30420 KB
#include <bits/stdc++.h>
#define endl "\n"

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

using namespace std;
using ll = long long;

ll inf = 5 * 1e9;

void pv(vector<vector<ll>> s) {
  for (auto c : s) {
    for (auto q : c) {
      cout << q << " ";
    }
    cout << endl;
  }
}

bool check(vector<ll>& s, ll w, ll p, ll q) {
  vector<vector<ll>> dp((int)s.size() + 1, vector<ll>(p + 1, 0));

  // храним что?
  // минимальное число использованных больших камер
  // dp[i][j] - мин.число бол.камер, которыми можно покрыть все события до i-го при условии, что остается еще j маленьких камер

  // dp[i][j - 1] = dp[k][j], где k находим бинпоиском по размеру большой камеры
  // dp[i][j] = dp[k][j] + 1, где k находим бинпоиском по размеру маленькой камеры

  for (int i = 0; i < s.size(); i++) {
    bool flag = false;
    ll l = 0, r = i + 1;
    while (r - l > 1) {
      ll mid = (r + l) / 2;
      if (s[i - mid] + w > s[i]) {
        l = mid;
      } else {
        r = mid;
      }
    }
    ll ind_small = i - l;

    l = 0;
    r = i + 1;
    while (r - l > 1) {
      ll mid = (r + l) / 2;
      if (s[i - mid] + 2 * w > s[i]) {
        l = mid;
      } else {
        r = mid;
      }
    }
    ll ind_big = i - l;
    for (int j = 0; j <= p; j++) {

      dp[i + 1][j] = inf;
      if (j > 0) dp[i + 1][j] = min(dp[i + 1][j], dp[ind_small][j - 1]);
      dp[i + 1][j] = min(dp[i + 1][j], dp[ind_big][j] + 1);

      if (dp[i + 1][j] <= q) flag = true;
    }
    if (!flag) return false;
  }

  // pv(dp);
  // cout << endl;

  return true;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  ll n, p, q;
  cin >> n >> p >> q;

  vector<ll> s(n);


  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }

  if (p + q >= n) {
    cout << "1" << endl;
    return 0;
  }

  sort(s.begin(), s.end());

  ll l = 0;
  ll r = inf;

  while (r - l > 1) {
    ll mid = (r + l) / 2;
    if (check(s, mid, p, q)) {
      r = mid;
    } else {
      l = mid;
    }
  }

  cout << r << endl;

  return 0;
}

Compilation message

watching.cpp: In function 'bool check(std::vector<long long int>&, ll, ll, ll)':
watching.cpp:31:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   31 |   for (int i = 0; i < s.size(); i++) {
      |                   ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 8 ms 604 KB Output is correct
8 Correct 33 ms 2484 KB Output is correct
9 Correct 195 ms 12476 KB Output is correct
10 Correct 530 ms 30420 KB Output is correct
11 Correct 25 ms 1928 KB Output is correct
12 Correct 263 ms 16072 KB Output is correct
13 Correct 8 ms 604 KB Output is correct
14 Correct 10 ms 820 KB Output is correct
15 Correct 10 ms 1044 KB Output is correct