A - スピーカーの音量 / Speaker Volume Editorial by admin
GLM 5.2 (High, OpenRouter)Overview
This problem asks us to find the total intensity of sound reaching a measurement point \(P\) from \(N\) speakers placed on a number line.
Analysis
As stated in the problem description, the sound intensity reaching the measurement point from the \(i\)-th speaker is represented as \(\frac{V_i}{|X_i - P|}\). If a speaker is located at the exact same coordinate as the measurement point (\(X_i = P\)), division by zero occurs, so it must be excluded from the calculation.
No special techniques or advanced algorithms are required; a straightforward approach of checking the condition and summing up the intensities for each speaker sequentially is sufficient. Given the constraints \(N \leq 2 \times 10^5\), an \(O(N)\) calculation processing one speaker at a time will comfortably run within the time limit.
Algorithm
- Input the number of speakers \(N\) and the coordinate of the measurement point \(P\).
- Initialize a variable (floating-point type) to hold the total sum with \(0\).
- For each speaker, input its coordinate \(X_i\) and output intensity \(V_i\), then perform the following:
- If \(X_i \neq P\), calculate the distance \(d = |X_i - P|\).
- Add \(\frac{V_i}{d}\) to the total sum variable.
- If \(X_i = P\), do nothing (exclude it).
- Finally, output the total sum. Since the acceptable error is \(10^{-4}\), output with a sufficient number of decimal places.
Complexity
- Time Complexity: \(O(N)\)
- Space Complexity: \(O(1)\)
Implementation Details
The coordinates \(P\) and \(X_i\) are integers between \(-10^9\) and \(10^9\), and the difference \(|X_i - P|\) can be up to \(2 \times 10^9\). Therefore, you should use a 64-bit integer type (
long long) instead of a 32-bit integer type (int).When performing division, cast either the dividend or the divisor to a floating-point number (
double) to avoid integer division.When printing the result, to satisfy the required precision (absolute error of at most \(10^{-4}\)), it is safer to specify a larger number of decimal places in your output.
Source Code
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
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) {
long long dist = (X > P) ? (X - P) : (P - X);
ans += static_cast<double>(V) / dist;
}
}
cout << fixed << setprecision(10) << ans << "\n";
return 0;
}
This editorial was generated by or-glm-5.2-high.
posted:
last update: