A - スピーカーの音量 / Speaker Volume 解説 by admin
claude4.8opus-highOverview
In this problem, we need to calculate \(\frac{V_i}{|X_i - P|}\) for each speaker and find the sum of these values. Speakers located at the exact same coordinate as the measurement point (\(X_i = P\)) should be excluded from the calculation.
Analysis
This problem does not require complex algorithms; it can be solved simply by directly executing the calculation defined in the problem statement.
The key points are as follows:
1. Each speaker can be calculated independently
The sound intensity that the \(i\)-th speaker delivers to the measurement point is \(\frac{V_i}{|X_i - P|}\), which can be calculated independently of the other speakers. Therefore, we simply calculate this value for each speaker one by one and sum them up.
2. Avoiding division by zero
When \(X_i = P\), the distance \(|X_i - P|\) becomes \(0\), making division impossible. In this case, we need to exclude that speaker from the calculation. Specifically, we include logic to skip when X == P.
For example, if the measurement point is \(P = 5\) and a speaker is at \(X = 5\), we ignore this speaker.
3. Handling absolute values
Since the distance is \(|X_i - P|\), it is always a positive value regardless of the sign of \(X_i - P\). For example, if \(X_i = 3, P = 5\), the distance is \(|3 - 5| = 2\); if \(X_i = 8, P = 5\), the distance is \(|8 - 5| = 3\). Be careful: if you forget to take the absolute value, the distance might become negative, leading to an incorrect result.
Algorithm
The algorithm is very simple:
- Initialize a variable
sumto \(0\) to store the total sum. - For each speaker \(i\), perform the following:
- If \(X_i = P\), skip it.
- Otherwise, calculate \(\frac{V_i}{|X_i - P|}\) and add it to
sum.
- Finally, print
sum.
Since the calculation result will be a real number (floating-point number), we use the double type. The problem statement allows an absolute error of at most \(10^{-4}\), so the precision of double is sufficient.
Complexity
- Time Complexity: \(O(N)\) (Process each speaker once)
- Space Complexity: \(O(1)\) (Only variables to keep track of the total sum, no need to store input in an array)
Since \(N \leq 2 \times 10^5\), this complexity easily passes within the time limit.
Implementation Details
Beware of overflow
Both \(X_i\) and \(P\) are in the range \(-10^9 \leq X_i, P \leq 10^9\), so the absolute value of \(X_i - P\) can be up to \(2 \times 10^9\). Since this is close to the limit of a 32-bit signed integer (int, around \(2.1 \times 10^9\)) and can be risky during intermediate calculations, it is safer to handle them using the long long type. In the code, P, X, and V are all read as long long.
Type casting for division
If you divide integers by integers, the fractional part will be truncated. Make sure to cast to (double) before performing the division.
sum += (double)V / (double)llabs(X-P);
By converting both the numerator and denominator to double like this, floating-point division is performed correctly.
Output precision
An absolute error of at most \(10^{-4}\) is allowed, but to be safe, it is good practice to print up to around \(10\) decimal places. In the code, this is done using printf("%.10f\n", sum);.
Source Code
#include <bits/stdc++.h>
using namespace std;
int main(){
int N;
long long P;
scanf("%d %lld", &N, &P);
double sum = 0.0;
for(int i=0;i<N;i++){
long long X, V;
scanf("%lld %lld", &X, &V);
if(X==P) continue;
sum += (double)V / (double)llabs(X-P);
}
printf("%.10f\n", sum);
return 0;
}
This editorial was generated by claude4.8opus-high.
投稿日時:
最終更新: