878. Halving the Largest Until the Total Fits
A warehouse holds n crates with given weights, and the floor is rated for a
total of t. One operation takes the HEAVIEST crate and replaces its weight
w by w / 2 rounded DOWN.
Print the fewest operations needed until the total weight is at most t.
If it already is, the answer is 0.
n = 4, t = 10, weights 8 6 4 2 -> 4
n = 3, t = 100, weights 1 2 3 -> 0
n = 2, t = 1, weights 7 7 -> 5
Trace the first one. The total is 20, which is over 10.
8 6 4 2 total 20 halve the 8 -> 4 6 4 2 total 16
4 6 4 2 total 16 halve the 6 -> 4 3 4 2 total 13
4 3 4 2 total 13 halve a 4 -> 2 3 4 2 total 11
2 3 4 2 total 11 halve the 4 -> 2 3 2 2 total 9
Nine is at most ten, so four operations.
Note that the operation always applies to the heaviest crate, so the crate it touches changes constantly as weights fall past one another.
## What most people try first
Halving the crates in sorted order, once each. This is wrong twice over.
A crate may need halving several times (7 needs five halvings to reach 0),
and after one halving the crate is usually no longer the heaviest.
Rescanning for the maximum every operation. Correct, and O(n) per
operation. The number of operations is bounded, but with n = 200000 crates
each needing up to 30 halvings that is 6 * 10^6 operations, and a linear
scan inside each one is 1.2 * 10^12 comparisons.
## The method
build a MAX-heap of the weights
total = sum of the weights
count = 0
while total > t:
w = pop the maximum
total = total - (w - w / 2)
push w / 2
count = count + 1
print count
A MAX-heap, because the operation is defined on the heaviest crate. With a library that only offers a min-heap, push the negated weights.
## Why it terminates
Every operation strictly reduces some positive weight to a strictly smaller
non-negative one, because w / 2 < w for every w >= 1 under floor
division. Once every weight reaches 0 the total is 0, which is at most t
since t is never negative. A weight of 10^9 reaches 0 in 30 halvings, so
the whole process runs at most 30 n times.
## Maintaining the total instead of recomputing it
Re-adding n weights after each operation would put the linear scan back.
Since exactly one crate changed, the new total is the old one minus
w - w / 2, which is what the crate lost. That subtraction is the same idea
as the running total in a sliding window: a total you maintain is cheaper than
a total you rebuild.
## Overflow, with the actual magnitude
The total is the part that overflows. Two hundred thousand crates at 10^9
each reach 2 * 10^14, far past the signed 32-bit limit of 2147483647.
And the failure is silent in the worst way: a wrapped total is often NEGATIVE,
so total > t is false immediately and the program prints 0 without doing any
work at all. The test with three crates of 10^9 and t = 100 is exactly
that case, and its true answer is 75.
Individual weights are fine in 32 bits. It is the sum that is not.
## The boundary that breaks a careless implementation
The total already being at most t. The loop must be a while tested
BEFORE the first operation, not a do ... while, or the second example
answers 1 instead of 0.
The other one is rounding: w / 2 rounds DOWN, so 7 becomes 3, not 4.
With negative weights that distinction would matter more, but weights here are
at least 1 so integer division and floor division agree.
## Related problems
Problem 877 is the mirror image on the minimum. Problem 892 is the same max-heap loop with a different transform and a score to accumulate, and the contrast between counting operations and accumulating a value is the whole difference.
## Cost
Time O(n + C log n) where C is the number of operations, at most
30 n. Space O(n).
Constraints - `1 ≤ n ≤ 200000` - `0 ≤ t ≤ 100000000000000` - `1 ≤ w ≤ 1000000000`
Input
The first line contains two integers n and t.
The second line contains n integers, the crate weights.
Output
Print one integer alone on a line: the fewest halving operations needed to bring the total weight down to at most t. Print no other words.
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.