A - 水の均等化 / Equalizing Water Editorial by admin
Gemini 3.0 Flash (Thinking)Overview
This is a problem of determining whether the water in \(N\) tanks can be equalized by transferring water between adjacent tanks.
Analysis
The key points for solving this problem are focusing on “the total amount of water in the entire system does not change through operations” and “the degree of freedom in water movement”.
1. Total Water Amount is Invariant (Invariant)
In the operation of transferring water between adjacent tanks, the amount that decreases in one tank increases by the same amount in the other tank. Therefore, the total sum of water across all tanks \(S = \sum A_i\) remains unchanged before and after any operation.
2. Water Can Be Transferred Between Any Tanks
Although operations are limited to “between adjacent tanks,” by repeating these operations, we can transport water along a path. For example, to move water from tank 1 to tank 3, we can route it as “1 → 2 → 3.” In other words, with a sufficient number of operations, water can be freely moved from any tank to any other tank (as long as water is available).
3. Condition for Equalization
To make the water amount in all tanks equal, each tank must ultimately contain \(S / N\) liters. From the problem’s constraints, we can observe the following: - The initial water amount in each tank is an integer. - Operations are performed in units of 1 liter.
Therefore, the final water amount \(S / N\) in each tank must also be an integer. If \(S\) is not divisible by \(N\), it is impossible to distribute the water perfectly equally using operations in units of 1 liter, no matter how the water is moved. Conversely, if \(S\) is divisible by \(N\), we can always adjust all tanks to \(S / N\) liters by sequentially transferring water from tanks with excess to tanks with deficit.
From the above, this problem reduces to “determining whether the total water amount \(S\) across all tanks is divisible by the number of tanks \(N\)”.
Algorithm
- Compute the sum \(S\) of the \(N\) given integers.
- Check whether the remainder of \(S\) divided by \(N\) is \(0\).
- If the remainder is \(0\), output
Yes; otherwise, outputNo.
Complexity
- Time Complexity: \(O(N)\)
- Computing the sum of \(N\) elements takes \(O(N)\).
- Space Complexity: \(O(N)\)
- If the \(N\) input elements are stored in a list or similar structure, \(O(N)\) memory is used.
Implementation Notes
In Python, the
inttype handles arbitrary-precision (infinite-precision) integers, so even if the sum \(S\) becomes a very large value (up to approximately \(10^{14}\)), you can compute it without worrying about overflow.When reading a large amount of input, using
sys.stdin.read().split()can sometimes process it faster.Source Code
import sys
def solve():
# 標準入力からすべてのデータを読み込み、空白で分割します。
# Nが最大10^5、各Aiが最大10^9であるため、この方法は効率的です。
input_data = sys.stdin.read().split()
if not input_data:
return
# 最初の要素はタンクの数 N です。
n = int(input_data[0])
# 残りの要素は各タンクの水量 A_i です。
# 水の移動は1リットル単位で行われ、開始時の水量も整数であるため、
# 最終的にすべてのタンクの水量を等しくするには、その値が整数である必要があります。
# つまり、すべてのタンクの合計水量が N で割り切れることが必要十分条件となります。
# 隣り合うタンク間で双方向に水を移動できるため、
# 合計水量が N の倍数であれば、必ずすべてのタンクの値を均等(合計/N)にできます。
# 合計水量を計算します(Pythonのint型は任意精度であるため、大きな和も扱えます)。
total_water = sum(map(int, input_data[1:n+1]))
# 合計水量が N で割り切れるかどうかを判定します。
if total_water % n == 0:
print("Yes")
else:
print("No")
if __name__ == '__main__':
solve()
This editorial was generated by gemini-3-flash-thinking.
posted:
last update: