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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, p, q, A[2005], w, upper, lower, dp[2005][2005];
ll f(ll index, ll bigCamsNeeded) { //Min p that can cover everything
if (index < 0) return 0;
if (dp[index][bigCamsNeeded] != -1) return dp[index][bigCamsNeeded]; //If calculated
dp[index][bigCamsNeeded] = LLONG_MAX;
if (bigCamsNeeded > 0) {
ll ind = upper_bound(A, A + n, A[index] - w - w) - A - 1; //Cannot take
dp[index][bigCamsNeeded] = f(ind, bigCamsNeeded - 1);
}
ll ind = upper_bound(A, A + n, A[index] - w) - A - 1;
dp[index][bigCamsNeeded] = min(dp[index][bigCamsNeeded], f(ind, bigCamsNeeded) + 1); // You used one more small camera so need + 1
return dp[index][bigCamsNeeded];
}
int main() {
cin >> n >> p >> q;
if (p + q >= n) {
cout << 1;
return 0;
}
for (ll i = 0; i < n; i++) cin >> A[i];
sort(A, A + n);
lower = 0; upper = A[n - 1] + 1;
while (upper - lower > 1) {
memset(dp, -1, sizeof dp); //Not calculated before.
w = lower + (upper - lower) / 2;
if (f(n - 1, q) <= p) upper = w;
else lower = w;
}
cout << upper;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |