877. Feeding the Smallest Pile
n piles of grain have given sizes. A conveyor runs for k rounds, and in
each round it drops exactly m more units onto whichever pile is SMALLEST at
that moment.
Print the n final sizes in non-decreasing order.
n = 4, k = 3, m = 5, piles 1 9 2 8 -> 7 8 9 11
n = 1, k = 4, m = 7, pile 10 -> 38
n = 3, k = 1, m = 1, piles 5 5 5 -> 5 5 6
Trace the first one. Round 1: the smallest is 1, it becomes 6, so the piles
are 6 9 2 8. Round 2: the smallest is 2, it becomes 7, giving 6 9 7 8.
Round 3: the smallest is 6, it becomes 11, giving 11 9 7 8. Sorted, that is
7 8 9 11.
When two piles tie for smallest it makes no difference which one is fed, since they are equal and the output is sorted.
## What most people try first
Sorting once and walking left to right. This is the mistake the problem
exists for. After a pile is fed it is no longer the smallest, and it may no
longer even be near the front. On 1 9 2 8 with m = 5, a sorted walk feeds
1, 2, 6 in that order only if it re-sorts; a single pass over the sorted
array 1 2 8 9 feeds 1, then 2, then 8, giving 6 7 13 9 which sorted is
6 7 9 13, not the right answer.
Re-scanning for the minimum every round. Correct, and O(n) per round.
With n = 100000 and k = 100000 that is 10^10 comparisons.
Re-sorting every round. Also correct, and worse: O(n log n) per round.
## The method
The set of candidates changes after every single round, and the change is
always the same shape: remove the minimum, put a modified version of it back.
That is exactly extract-min followed by insert, which a min-heap does in
O(log n).
build a min-heap of the pile sizes O(n)
repeat k times:
v = pop the minimum
push v + m
sort what is left and print
Many libraries offer the combined operation (heapreplace in Python,
overwriting the root and sifting down by hand elsewhere), which does one
sift instead of two.
## Why feeding the smallest really does need a heap
Nothing about the answer is greedy or clever; the rule simply says to feed the
smallest, and a heap is the structure that answers "which is smallest" while
the collection keeps changing. The reason a sorted array fails is worth being
precise about: a sorted array answers the query in constant time but pays
O(n) to re-insert the fed pile at its new position, and there are k such
re-insertions.
## The boundary that breaks a careless implementation
k = 0. The conveyor never runs and the answer is the input, sorted. A loop
written as do ... while feeds one pile anyway.
n = 1. There is only one pile and it receives everything: 10 + 4 * 7 = 38
in the second example. A program that tries to compare against a second pile
reads past the end.
## Overflow
A pile starts at 10^9 and can absorb every round: 10^9 + 10^5 * 10^9,
which is 1.000001 * 10^14. A signed 32-bit integer stops at
2147483647, so this genuinely overflows and the sizes must be 64-bit. Note
that m itself, at 10^9, fits in 32 bits perfectly well; it is the
accumulation that does not.
## Related problems
Problem 878 is the mirror image: it repeatedly modifies the LARGEST value and asks how many rounds are needed rather than what is left. Problem 892 also modifies the largest but accumulates a score.
## Cost
Time O(n + k log n), plus O(n log n) to sort the output. Space O(n).
Constraints - `1 ≤ n ≤ 100000` - `0 ≤ k ≤ 100000` - `1 ≤ m ≤ 1000000000` - `1 ≤ a[i] ≤ 1000000000`
Input
The first line contains three integers n, k and m.
The second line contains n integers, the initial pile sizes.
Output
Print the n final pile sizes in non-decreasing order, on one line, separated by single spaces. Print no other words.
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.