A - スピーカーの音量 / Speaker Volume Editorial by admin
gpt-5.6-sol-highOverview
For each speaker, if the distance to the measurement point is not \(0\), we calculate \(\frac{V_i}{|X_i-P|}\) and find their sum. To suppress floating-point addition errors, we use compensated summation.
Analysis
The sound intensity given to the measurement point by the \(i\)-th speaker is
\( \frac{V_i}{|X_i-P|} \)
Therefore, we just need to iterate through the given speakers in order and add this value. There is no need to consider combinations of speakers or to sort them by coordinates.
However, when \(X_i=P\), the distance becomes \(0\). According to the problem statement, this speaker must be excluded from the calculation. If division is performed without excluding it, a division-by-zero error will occur.
For example, suppose the measurement point is \(P=3\), and the speakers are placed as follows:
- \((X_1,V_1)=(1,4)\)
- \((X_2,V_2)=(3,10)\)
- \((X_3,V_3)=(7,8)\)
For each speaker:
- 1st speaker: \(\frac{4}{|1-3|}=\frac{4}{2}=2\)
- 2nd speaker: Excluded because \(X_2=P\)
- 3rd speaker: \(\frac{8}{|7-3|}=\frac{8}{4}=2\)
Thus, the answer is \(2+2=4\).
Floating-Point Errors
Since the answer contains fractions, it needs to be calculated using real numbers. However, with computer floating-point numbers, rounding errors can accumulate when adding many values together.
Therefore, this implementation uses compensated summation (Kahan summation). In addition to the standard sum total, the small loss of precision from previous additions is tracked in compensation. By subtracting this error from the next value to be added, we can obtain a total with higher accuracy than simple summation.
Algorithm
- Initialize the total
totalto \(0\) and the compensation valuecompensationto \(0\). - For each speaker, do the following:
- Calculate the distance \(d=|X_i-P|\).
- If \(d=0\), exclude this speaker from the calculation.
- Otherwise, add the sound intensity \(\frac{V_i}{d}\) to
totalusing compensated summation.
- Output
totalwith a sufficient number of decimal places.
In compensated summation, given the term \(a\) to be added, the values are updated as follows:
- \(y=a-\text{compensation}\)
- \(t=\text{total}+y\)
- \(\text{compensation}=(t-\text{total})-y\)
- \(\text{total}=t\)
If all speakers satisfy \(X_i=P\), no additions occur, and \(0\) is output directly.
Complexity
- Time complexity: \(O(N)\)
- Space complexity: \(O(1)\)
Implementation Points
Calculate the distance to the measurement point using
abs(x - p).If the distance is \(0\), skip it using
continuebefore performing division.Maintain
totalandcompensationas floating-point numbers and perform compensated summation.Outputting with a sufficient number of decimal places, such as
print(f"{total:.15f}"), makes it easier to satisfy the required error bound of \(10^{-4}\) or less.Since the number of inputs can be large (up to \(2\times 10^5\)), use fast I/O like
sys.stdin.buffer.readlineto read the input quickly.Source Code
import sys
def main():
input = sys.stdin.buffer.readline
n, p = map(int, input().split())
total = 0.0
compensation = 0.0
for _ in range(n):
x, v = map(int, input().split())
distance = abs(x - p)
if distance == 0:
continue
value = v / distance - compensation
new_total = total + value
compensation = (new_total - total) - value
total = new_total
print(f"{total:.15f}")
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.6-sol-high.
posted:
last update: