公式

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

claude4.8opus-high

Overview

This problem asks us to calculate the sum of the sound intensity \(\frac{V_i}{|X_i - P|}\) reaching a measurement point \(P\) from \(N\) speakers placed on a number line. However, speakers with \(X_i = P\) must be excluded from the calculation.

Analysis

This is a simple problem where we just need to calculate the sound intensity for each speaker independently and sum them up. No special algorithms or data structures are required.

There are two important points to keep in mind:

1. Avoiding Division by Zero

When a speaker is located at the same coordinate as the measurement point (\(X_i = P\)), the distance becomes \(0\), and \(\frac{V_i}{|X_i - P|}\) cannot be calculated. We must exclude such speakers from the sum.

Specifically, calculate \(d = X_i - P\), and if \(d = 0\), skip that speaker. Even if all speakers are excluded, the initial total is \(0\), so outputting \(0\) gives the correct result.

2. Handling Absolute Values

Distance is the absolute value \(|X_i - P|\). Depending on the sign of \(d = X_i - P\), the distance is obtained as follows:

  • When \(d > 0\), the distance is \(d\)
  • When \(d < 0\), the distance is \(-d\)

For example, if \(P = 5\), \(X_i = 3\), and \(V_i = 10\), then \(d = 3 - 5 = -2\), so the distance is \(2\), and the sound intensity becomes \(\frac{10}{2} = 5\).

Algorithm

We process each speaker one by one:

  1. Initialize the total sum total to \(0\).
  2. For each speaker \(i\):
    • Calculate \(d = X_i - P\).
    • If \(d \neq 0\), find the distance \(|d|\) and add \(\frac{V_i}{|d|}\) to total.
    • If \(d = 0\), do nothing (skip).
  3. Finally, print total.

Simply doing this naively for all \(N\) speakers yields the answer.

Complexity

  • Time Complexity: \(O(N)\)
  • Space Complexity: \(O(N)\) (if reading all inputs at once; \(O(1)\) if processed sequentially)

Since \(N \leq 2 \times 10^5\), an \(O(N)\) approach is well within the time limit.

Implementation Details

Fast I/O

Since \(N\) is relatively large (up to \(2 \times 10^5\)), calling input() line by line might be slow. Reading all inputs at once using sys.stdin.buffer.read() and processing them together speeds up the execution.

Calculating Absolute Values

In Python, you can compute absolute values using abs(d), but using a conditional expression like d if d > 0 else -d also works fine. Either method is acceptable.

Precision and Error

An answer is accepted as long as the absolute error is at most \(10^{-4}\). Computing with floating-point numbers (float) provides sufficient precision, so no special measures are required. Python’s print function outputs enough digits by default.

Source Code

import sys

def main():
    data = sys.stdin.buffer.read().split()
    idx = 0
    N = int(data[idx]); idx += 1
    P = int(data[idx]); idx += 1
    total = 0.0
    for _ in range(N):
        X = int(data[idx]); idx += 1
        V = int(data[idx]); idx += 1
        d = X - P
        if d != 0:
            total += V / (d if d > 0 else -d)
    print(total)

main()

This editorial was generated by claude4.8opus-high.

投稿日時:
最終更新: