Official

A - スピーカーの音量 / Speaker Volume Editorial by admin

gemini-3.6-flash-high

Overview

This problem asks us to calculate the total intensity of sound reaching a measurement point \(P\) from speakers placed on a number line.

Analysis

We consider calculating the sound intensity for each speaker and summing them up as instructed in the problem statement.

For the \(i\)-th speaker, the intensity of the sound reaching the measurement point \(P\) is determined as follows: - When \(X_i \neq P\): \(\frac{V_i}{|X_i - P|}\) - When \(X_i = P\): Exclude from calculation (treat the intensity as \(0\))

The number of speakers \(N\) is at most \(2 \times 10^5\). Since the processing for each speaker (distance calculation, exclusion check, division, addition) can be done in constant time \(O(1)\), a simple loop checking all \(N\) speakers sequentially will easily finish within the time limit.

In addition, the allowed error in the output is specified as \(10^{-4}\) or less. Using double-precision floating-point numbers in standard programming languages (such as float in Python) allows us to perform the calculation with sufficient accuracy.

Algorithm

  1. Initialize a variable ans to hold the answer with 0.0.
  2. Obtain the information of \(N\) speakers \((X_i, V_i)\) sequentially from the input.
  3. If \(X_i \neq P\), add \(\frac{V_i}{|X_i - P|}\) to ans. If \(X_i = P\), do nothing.
  4. After processing all speakers, output ans with sufficient precision (a few to a dozen decimal places).

Complexity

  • Time Complexity: \(O(N)\)
    • Since we perform \(O(1)\) checks and calculations for each of the \(N\) speakers, the overall time complexity is \(O(N)\). With \(N \le 2 \times 10^5\), this will easily run well within the time limit.
  • Space Complexity: \(O(N)\)
    • Uses \(O(N)\) memory to read all the input data at once. (If processed line by line, it can be reduced to \(O(1)\).)

Implementation Notes

  • Avoiding division by zero: When \(X_i = P\), \(|X_i - P| = 0\), and performing division directly will cause an error (division by zero). Make sure to exclude this case using a conditional branch like if X_i != P:.

  • Fast I/O: In Python, calling input() \(N\) times may take a long time to execute. Using sys.stdin.read().split() allows reading a large amount of input efficiently.

    Source Code

import sys


def main():
    input_data = sys.stdin.read().split()
    if not input_data:
        return

    it = iter(input_data)
    N = int(next(it))
    P = int(next(it))

    ans = 0.0
    for _ in range(N):
        x = int(next(it))
        v = int(next(it))
        if x != P:
            ans += v / abs(x - P)

    print(f"{ans:.12f}")


if __name__ == "__main__":
    main()

This editorial was generated by gemini-3.6-flash-high.

posted:
last update: