A - 水の補充 / Refilling Water Editorial by admin
Qwen3-Coder-480BOverview
Water the flowerpots in the given order and determine the final water amount in each flowerpot.
Analysis
This problem is a straightforward simulation where we look at the flowerpots in order from the beginning and pour water as needed.
For each flowerpot \(i\), it already contains \(S_i\) liters and has a maximum capacity of \(C_i\), so the additional amount of water that can be poured is \(C_i - S_i\). We manage the remaining amount of water we have as remaining and compare it with this “amount that can be poured” to process accordingly.
The key is the following two cases:
- If the remaining water is sufficient to fill that flowerpot, the flowerpot becomes full (\(C_i\)), and the remaining water decreases by remaining -= (C_i - S_i).
- If the remaining water is not enough, the flowerpot receives \(S_i + remaining\) amount of water, and since there is no more water, we set remaining = 0.
Since we perform this process sequentially from the beginning, no special data structures or algorithms are needed.
The reason a naive approach is fast enough is that we only perform constant-time processing for each flowerpot. However, it may be necessary to use techniques for reading input quickly (especially when \(N\) is large).
Algorithm
- Read all input and store the capacity \(C_i\) and current water amount \(S_i\) of each flowerpot in arrays.
- Initialize the remaining water amount
remainingto \(M\). - For each flowerpot, do the following:
- Compute the needed water amount \(needed = C_i - S_i\).
- If
remaining >= needed, the flowerpot becomes full (\(C_i\)), and decreaseremaining. - Otherwise, the flowerpot receives \(S_i + remaining\) amount of water, and set
remainingto 0.
- Output the final water amount of each flowerpot.
Complexity
- Time complexity: \(O(N)\)
- Space complexity: \(O(N)\)
Implementation Notes
Since the input is very large, it is efficient to read it all at once using
sys.stdin.read.Since each flowerpot is processed in constant time, avoid unnecessary computations inside the loop.
It is faster to store the final results in a list and output them all at once at the end.
Source Code
import sys
input = sys.stdin.read
def main():
data = input().split()
N = int(data[0])
M = int(data[1])
C = [0] * N
S = [0] * N
index = 2
for i in range(N):
C[i] = int(data[index])
S[i] = int(data[index+1])
index += 2
remaining = M
result = []
for i in range(N):
needed = C[i] - S[i]
if remaining >= needed:
result.append(C[i])
remaining -= needed
else:
result.append(S[i] + remaining)
remaining = 0
for r in result:
print(r)
if __name__ == "__main__":
main()
This editorial was generated by qwen3-coder-480b.
posted:
last update: