# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
503106 | colossal_pepe | Watching (JOI13_watching) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n, p, q;
vector<int> v;
int brutus(int w, int i, int p_left, vector<vector<int>> &dp) {
if (i >= n) return 0;
if (dp[i][p_left] != -1) return dp[i][p_left];
int useq = lower_bound(v.begin(), v.end(), v[i] + (2 * w)) - v.begin();
dp[i][p_left] = brutus(w, useq, p_left, dp) + 1;
if (p_left) {
int usep = lower_bound(v.begin(), v.end(), v[i] + w) - v.begin();
dp[i][p_left] = min(dp[i][p_left], brutus(w, usep, p_left - 1, dp));
}
return dp[i][p_left];
}
int solve() {
vector<vector<int>> dp(n, vector<int>(p + 1, -1));
int minw = 1, maxw = 1e9;
while (maxw - minw >= 5) {
int mid = (maxw + minw) / 2;
fill(dp.begin(), dp.end(), -1);
if (brutus(mid, 0, p, dp) <= q) maxw = mid;
else minw = mid + 1;
}
for (int i = minw; i <= maxw; i++) {
fill(dp.begin(), dp.end(), -1);
if (brutus(i, 0, p, dp) <= q) return i;
}
return 1e9;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> p >> q;
v.resize(n);
for (int &i : v) {
cin >> i;
}
sort(v.begin(), v.end());
cout << solve() << '\n';
return 0;
}