#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;
for (int j = 0; j <= p; j++) {
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;
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++) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
5 ms |
344 KB |
Output is correct |
12 |
Correct |
4 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
348 KB |
Output is correct |
2 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
53 ms |
604 KB |
Output is correct |
8 |
Correct |
424 ms |
2660 KB |
Output is correct |
9 |
Execution timed out |
1059 ms |
12396 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |