A - 水の均等化 / Equalizing Water Editorial by admin
GPT 5.2 HighOverview
When water can be transferred 1 liter at a time between adjacent tanks, we determine whether all tanks can be made to have the same amount of water. The conclusion is that it suffices to check only “whether the total amount of water is divisible by \(N\).”
Discussion
First, with this operation, the total amount of water across all tanks never changes (the amount decreased from one tank is simply increased in its neighbor).
Therefore, to make all tanks have the same amount of water \(x\) in the end:
- The total \(S=\sum A_i\) must equal \(Nx\)
- In other words, \(S\) must be divisible by \(N\) and \(x=S/N\) must be an integer
This is a necessary condition.
Next, we confirm that this is also a sufficient condition (i.e., if divisible, it can always be achieved). The key point is that although “water can only be moved between adjacent tanks” appears to be a constraint, since the tanks are connected in a line, water can be transported in order both to the right and to the left, ultimately adjusting everything to the average \(x\).
Intuitively, if we scan from left to right:
- If tank \(i\) has more than the average \(x\), send the excess to the right
- If it has less, bring the needed amount from the right (moving 1 liter at a time from right to left)
By repeating this, all tanks can eventually reach \(x\).
The constraint “you cannot take water from a tank with 0 liters” is also not a problem. Even when transporting to the left, you can pass water 1 liter at a time from the side where water exists to its neighbor, so transportation is possible even if intermediate tanks temporarily reach 0.
Concrete example: - \(A=[0,3,0],\ N=3,\ S=3,\ x=1\) (divisible, so it’s possible) - Move 1 liter from tank 2 to tank 1 → \([1,2,0]\) - Move 1 liter from tank 2 to tank 3 → \([1,1,1]\)
Conversely, if \(S\) is not divisible by \(N\), since the final amounts must be integers, it is impossible for all tanks to have the same amount.
Therefore, the determination is simply \(S \bmod N == 0\).
A naive approach of “actually simulating the operations” would not work in time (TLE), since water amounts can be up to \(10^9\) and the number of 1-liter transfers would be enormous. By only checking the remainder of the total, we can determine the answer instantly.
Algorithm
- Compute the total amount of water \(S=\sum_{i=1}^{N} A_i\)
- If \(S \bmod N == 0\), output
Yes; otherwise, outputNo
Complexity
- Time complexity: \(O(N)\) (just computing the sum)
- Space complexity: \(O(1)\) (constant extra space aside from the input array)
Implementation Notes
In Python, the total can be as large as \(10^5 \times 10^9=10^{14}\), but since
inthas arbitrary precision, it can be handled directly.Since the input can be large, using
sys.stdin.readlineis a safe choice.Source Code
import sys
def main():
input = sys.stdin.readline
N = int(input().strip())
A = list(map(int, input().split()))
s = sum(A)
print("Yes" if s % N == 0 else "No")
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.2-high.
posted:
last update: