A - スピーカーの音量 / Speaker Volume Editorial by admin
GLM 5.2 (High, OpenRouter)Overview
You are given \(N\) speakers on a number line and a measurement point \(P\). You need to find the sum of sound intensities \(\frac{V_i}{|X_i - P|}\) from all speakers except those located at the same coordinate as the measurement point.
Analysis
This problem can be solved by directly implementing the formula given in the statement.
For each speaker, calculate its distance \(|X_i - P|\) to the measurement point \(P\). If this distance is \(0\) (i.e., when \(X_i = P\)), exclude it from the calculation to avoid division by zero. Only when the distance is non-zero, add \(\frac{V_i}{|X_i - P|}\) to the total sum.
No special tricks or advanced algorithms are required; a naive approach that iterates through all \(N\) speakers one by one will easily pass within the time limit. However, if you are using Python, large I/O can be slow, so it is safe to speed up input reading by reading all input at once using sys.stdin.buffer.read().split().
Algorithm
- Read the number of speakers \(N\) and the coordinate of the measurement point \(P\).
- Initialize a variable
ansto0.0to store the total sound intensity. - For each speaker \(i\), repeat the following steps:
- Read the speaker’s coordinate \(X_i\) and output volume \(V_i\).
- Compute the difference \(d = X_i - P\).
- If \(d > 0\), add \(\frac{V_i}{d}\) to
ans. - If \(d < 0\), add \(\frac{V_i}{-d}\) to
ans. - If \(d = 0\), do nothing.
- Output
ans.
Complexity
- Time Complexity: \(O(N)\)
- Space Complexity: \(O(N)\) (due to storing the input data as an array)
Implementation Notes
When calculating distance, although you could use
abs(X - P)and check if it equals zero, branching based on the sign ofX - Pavoids redundant calculations while naturally skipping the case where \(X_i = P\).When performing division in Python, make sure to use the
/operator to compute floating-point numbers. Using the//operator (integer division) will not yield the correct result.By initializing the total sum to
0.0, the value is implicitly treated as a floating-point number throughout the calculation.Source Code
import sys
def solve():
data = sys.stdin.buffer.read().split()
if not data:
return
N = int(data[0])
P = int(data[1])
ans = 0.0
idx = 2
for _ in range(N):
X = int(data[idx])
V = int(data[idx + 1])
dist = X - P
if dist > 0:
ans += V / dist
elif dist < 0:
ans += V / (-dist)
idx += 2
print(ans)
if __name__ == "__main__":
solve()
This editorial was generated by or-glm-5.2-high.
posted:
last update: