Official

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

gemini-3.6-flash-high

Summary

This problem asks us to find the sum of the sound intensities \(\frac{V_i}{|X_i - P|}\) reaching a measurement point \(P\) from speakers located at positions on a number line different from \(P\), among \(N\) total speakers.

Analysis

We consider faithfully executing the calculation formula given in the problem statement.

For each speaker \(i\) (\(1 \leq i \leq N\)), we perform the following check: - When \(X_i = P\): The distance becomes \(0\), and the sound intensity cannot be defined, so we ignore it (do not add to the sum). - When \(X_i \neq P\): The distance is \(|X_i - P|\), so we calculate the sound intensity \(\frac{V_i}{|X_i - P|}\) and add it to the sum.

The number of speakers \(N\) is at most \(2 \times 10^5\). Processing each speaker one by one to calculate and sum up the sound intensities will easily complete well within the time limit.

Also, since the answer is a decimal value, we need to perform the calculations using floating-point numbers (such as the double type).

Algorithm

  1. Initialize a variable ans to 0.0 to store the total sum.
  2. Read the information for the 1st through \(N\)-th speakers one by one.
    • Receive the coordinate \(X_i\) and output power \(V_i\).
    • If \(X_i = P\), do nothing and proceed to the next speaker.
    • If \(X_i \neq P\), calculate the distance \(d = |X_i - P|\) and add \(\frac{V_i}{d}\) to ans.
  3. After processing all speakers, output the value of ans with sufficient precision (for example, 15 decimal places).

Complexity

  • Time Complexity: \(O(N)\)
    • Since the calculation for one speaker takes \(O(1)\) constant time, the overall processing for \(N\) speakers takes \(O(N)\). Given \(N \le 2 \times 10^5\), this is sufficiently fast.
  • Space Complexity: \(O(1)\)
    • By processing the input on the fly without storing it in an array or similar data structure, the additional memory required is only for a constant number of variables.

Implementation Details

  • Avoiding division by zero: When \(X_i = P\), the distance is \(0\), and dividing by it directly will cause the program to crash (division by zero). Make sure to exclude this case using a conditional branch.

  • Output precision: The problem statement mentions that the answer is considered correct if the absolute error is at most \(10^{-4}\). Since C++’s std::cout may output only a few digits by default, specify fixed and setprecision(15) to output a sufficient number of decimal places.

  • Choice of data types: The absolute values of coordinates \(X_i\) and \(P\) can be up to \(10^9\). To prevent overflow during subtraction, it is safe to use a 64-bit integer type (such as long long in C++).

    Source Code

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    long long p;
    if (!(cin >> n >> p)) return 0;

    double ans = 0.0;
    for (int i = 0; i < n; ++i) {
        long long x, v;
        cin >> x >> v;
        if (x == p) continue;
        long long dist = std::abs(x - p);
        ans += (double)v / dist;
    }

    cout << fixed << setprecision(15) << ans << "\n";

    return 0;
}

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

posted:
last update: