Official

A - 水の均等化 / Equalizing Water Editorial by admin

Claude 4.6 Opus (Thinking)

Overview

This is a problem of determining whether the total water in \(N\) tanks can be evenly divided into \(N\) equal parts. Since water can be freely moved between adjacent tanks, whether the water can ultimately be equalized depends solely on whether the total is divisible by \(N\).

Analysis

Key Insight: Freedom of Operations

At first glance, the constraint “water can only be moved between adjacent tanks” seems restrictive. However, upon closer thought, by using adjacent tanks as relay points, water can be transported from any tank to any other tank.

For example, if you want to move \(1\) liter of water from tank \(1\) to tank \(3\): 1. Move \(1\) liter from tank \(1\) → tank \(2\) 2. Move \(1\) liter from tank \(2\) → tank \(3\)

This leaves tank \(2\)’s water level unchanged, effectively achieving the same result as directly moving water from tank \(1\) to tank \(3\).

The Essence of the Problem

Being able to perform operations any number of times and freely move water to any location means that, under the condition that the total amount of water is conserved, any desired distribution can be achieved.

To make all tanks have equal water levels, each tank needs to contain \(\frac{\sum A_i}{N}\) liters. If this is not an integer, it is impossible to achieve (since operations are in units of \(1\) liter, water levels are always integers).

Therefore, the condition is:

\[\sum_{i=1}^{N} A_i \mod N = 0\]

Why Naive Simulation Doesn’t Work

If you try to simulate the water movement, since \(A_i\) can be as large as \(10^9\), moving water \(1\) liter at a time would require an enormous number of operations, resulting in TLE. Based on the analysis above, simulation is unnecessary — checking divisibility alone is sufficient.

Algorithm

  1. Read the \(N\) water amounts \(A_1, A_2, \ldots, A_N\)
  2. Compute the total \(S = \sum_{i=1}^{N} A_i\)
  3. If \(S \mod N = 0\), output Yes; otherwise, output No

Concrete Examples

  • \(N = 3\), \(A = [1, 2, 3]\): total \(= 6\), \(6 \mod 3 = 0\)Yes (each tank can have \(2\) liters)
  • \(N = 3\), \(A = [1, 2, 4]\): total \(= 7\), \(7 \mod 3 = 1 \neq 0\)No (cannot be divided equally)

Complexity

  • Time complexity: \(O(N)\) (scanning \(N\) elements to compute the sum of the array)
  • Space complexity: \(O(N)\) (to store the array \(A\))

Implementation Notes

  • Since \(A_i\) can be up to \(10^9\) and \(N\) can be up to \(10^5\), the total can be up to around \(10^{14}\). In Python, there is no need to worry about integer overflow, but if using C++ or similar languages, make sure to use the long long type.

  • The essence of the problem is the simple check “is the total divisible by \(N\)” — there is no need to actually simulate the water movement.

    Source Code

N = int(input())
A = list(map(int, input().split()))
print("Yes" if sum(A) % N == 0 else "No")

This editorial was generated by claude4.6opus-thinking.

posted:
last update: