公式

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

gpt-5.6-sol-high

Overview

For each speaker located at a coordinate different from the measurement point \(P\), calculate the reaching sound intensity \(\frac{V_i}{|X_i-P|}\) and find their total sum.

Analysis

The sound intensity that each speaker provides to the measurement point is independent of the other speakers. Therefore, there is no need to check combinations of speakers; it is sufficient to process the input speakers one by one and add them up.

For the \(i\)-th speaker, if \(X_i \neq P\), the distance is

\(|X_i-P|\)

so we add

\(\frac{V_i}{|X_i-P|}\)

to the answer.

On the other hand, if \(X_i=P\), the distance becomes \(0\), resulting in a division-by-zero error. As instructed in the problem statement, we exclude this speaker from the calculation.

For example, when \(P=3\), suppose we have the following speakers:

  • \((X,V)=(1,4)\): Reaching intensity is \(\frac{4}{|1-3|}=2\)
  • \((X,V)=(3,10)\): Excluded because \(X=P\)
  • \((X,V)=(7,8)\): Reaching intensity is \(\frac{8}{|7-3|}=2\)

Therefore, the total sum is \(2+2=4\).

In this problem, a simple linear search—checking every speaker once—is sufficiently fast. However, the following implementation mistakes may lead to a Wrong Answer (WA):

  • Calculating even when \(X_i=P\), causing a division by zero
  • Performing integer division, truncating the fractional part
  • Outputting too few decimal digits, exceeding the allowable error threshold

Algorithm

  1. Initialize the answer to \(0\).
  2. For each speaker, read its coordinate \(X_i\) and volume \(V_i\).
  3. If \(X_i=P\), ignore that speaker.
  4. Otherwise, calculate the distance \(|X_i-P|\).
  5. Add \(\frac{V_i}{|X_i-P|}\) to the answer as a floating-point number.
  6. Output the answer formatted to a sufficient number of decimal places.

If \(X_i=P\) for all speakers, no addition will be performed, and \(0\) will be output directly.

Complexity

  • Time Complexity: \(O(N)\)
  • Space Complexity: \(O(1)\)

Implementation Notes

  • Use long long for coordinate and distance calculations to safely compute the differences.

  • To prevent V / distance from performing integer division, cast V to long double before dividing.

  • Store the answer in a long double to keep numerical errors sufficiently small.

  • Specify fixed << setprecision(15) to output a sufficient number of digits after the decimal point.

    Source Code

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N;
    long long P;
    cin >> N >> P;

    long double answer = 0.0L;

    for (int i = 0; i < N; ++i) {
        long long X, V;
        cin >> X >> V;

        if (X != P) {
            long long distance = (X > P ? X - P : P - X);
            answer += static_cast<long double>(V) / distance;
        }
    }

    cout << fixed << setprecision(15) << answer << '\n';
    return 0;
}

This editorial was generated by gpt-5.6-sol-high.

投稿日時:
最終更新: