# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1115566 | vjudge1 | Rice Hub (IOI11_ricehub) | 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 <bits/stdc++.h>
using namespace std;
using ll = long long;
bool isFeasible(int m, int R, vector<ll>& X, ll B, vector<ll>& prefix_sum) {
if (m == 0) return true;
int median_pos = (m - 1) / 2;
ll min_sum = LLONG_MAX;
for (int l = 0; l <= R - m; ++l) {
int r = l + m - 1;
int med = l + median_pos;
ll sum_left = X[med] * (med - l) - (prefix_sum[med - 1] - (l > 0 ? prefix_sum[l - 1] : 0));
ll sum_right = (prefix_sum[r] - prefix_sum[med]) - X[med] * (r - med);
ll total_sum = sum_left + sum_right;
if (total_sum < min_sum) {
min_sum = total_sum;
if (min_sum <= B) break;
}
}
return min_sum <= B;
}
int besthub(int R, int L, vector<int>& X, ll B) {
vector<ll> X_ll(R);
for (int i = 0; i < R; ++i) X_ll[i] = X[i];
vector<ll> prefix_sum(R + 1, 0);
for (int i = 1; i <= R; ++i) prefix_sum[i] = prefix_sum[i - 1] + X_ll[i - 1];
int low = 1, high = R;
int result = 1;
while (low <= high) {
int mid = (low + high) / 2;
if (isFeasible(mid, R, X_ll, B, prefix_sum)) {
result = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
return result;
}
int main() {
int R, L;
ll B;
cin >> R >> L >> B;
vector<int> X(R);
for (int i = 0; i < R; ++i) cin >> X[i];
sort(X.begin(), X.end());
int max_m = besthub(R, L, X, B);
cout << max_m << endl;
return 0;
}