Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
Marked verified and guaranteed to pass.
Hints
Open only as much as you need to keep the solve alive.
Sort the array first. Then the median is simply the element at index in -based indexing.
Only the median and elements to its right are worth thinking about. Increasing something in the left half is usually just paying extra to turn a weaker element into a candidate.
For a target value , the condition “median is at least ” is equivalent to having at least elements that are .
After sorting, the cheapest elements to make at least are the suffix . The needed operations are
The predicate is monotonic: if you can make the median , you can make it any smaller value too. Binary search the largest feasible in .
Sort the array. Let
be the median index in -based indexing. Initially the median is .
We want to maximize the median using only increments. The tempting wrong move is to randomly pump numbers in the left half. Don't. That's usually burning operations for no reason.
Suppose we ask a simpler question:
Can we make the median at least using at most operations?
For an odd-sized array, median means at least elements are .
After sorting, the best candidates for these elements are the largest elements:
Why? Because increasing a larger number to is never more expensive than increasing a smaller number to . If you choose some element from the left half instead of one from this suffix, you're choosing the worse deal. Classic competitive programming coupon logic: take the discount, don't be a hero.
So the minimum number of operations needed to make the median at least is
If , then is feasible.
If we can make the median at least , then we can definitely make it at least any smaller value .
So feasibility is monotonic:
This screams binary search. Loudly. With a megaphone.
The answer is at least , and it can never exceed , because raising the median by more than would require more than total increments already for the median candidate alone.
So binary search on:
Sorting costs:
Each feasibility check scans the suffix, costing , and binary search needs about iterations because values are up to around .
Total complexity:
which is easily fine for .
Memory usage is:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int n;
ll k;
cin >> n >> k;
vector<ll> a(n);
for (ll &x : a) cin >> x;
sort(a.begin(), a.end());
int m = n / 2;
auto can = [&](ll x) {
ll need = 0;
for (int i = m; i < n; i++) {
if (a[i] < x) need += x - a[i];
if (need > k) return false;
}
return need <= k;
};
ll lo = a[m], hi = a[m] + k + 1; // [lo, hi), hi is impossible
while (hi - lo > 1) {
ll mid = lo + (hi - lo) / 2;
if (can(mid)) lo = mid;
else hi = mid;
}
cout << lo << '\n';
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int n;
ll k;
cin >> n >> k;
vector<ll> a(n);
for (ll &x : a) cin >> x;
sort(a.begin(), a.end());
int m = n / 2;
auto can = [&](ll x) {
ll need = 0;
for (int i = m; i < n; i++) {
if (a[i] < x) need += x - a[i];
if (need > k) return false;
}
return need <= k;
};
ll lo = a[m], hi = a[m] + k + 1; // [lo, hi), hi is impossible
while (hi - lo > 1) {
ll mid = lo + (hi - lo) / 2;
if (can(mid)) lo = mid;
else hi = mid;
}
cout << lo << '\n';
return 0;
}