A - スピーカーの音量 / Speaker Volume 解説 by admin
gpt-5.6-sol-xhighOverview
For each speaker, if the distance to the measurement point is not \(0\), calculate \(\frac{V_i}{|X_i-P|}\) and sum these values up.
Analysis
The intensity of sound reaching the measurement point from each speaker can be calculated independently of the other speakers. Therefore, there is no need to check all combinations of speakers or to sort them by coordinates.
The distance between the \(i\)-th speaker and the measurement point is
\(|X_i-P|\)
. If the distance is not \(0\), we add the sound intensity from that speaker,
\(\frac{V_i}{|X_i-P|}\)
, to the total.
On the other hand, if \(X_i=P\), the denominator becomes \(0\). Since the problem statement specifies that such speakers should be excluded from the calculation, we need to check whether the distance is \(0\) before performing division. Dividing without checking will result in a runtime error due to division by zero.
For example, suppose the measurement point is \(P=3\), and the speakers are placed as follows:
- \((X,V)=(1,4)\): The intensity is \(\frac{4}{|1-3|}=2\)
- \((X,V)=(3,10)\): Excluded because it is at the same coordinate as the measurement point
- \((X,V)=(7,8)\): The intensity is \(\frac{8}{|7-3|}=2\)
Therefore, the total is \(2+2=4\).
Since we only need to examine each speaker once, an \(O(N)\) approach is sufficient. An \(O(N^2)\) process that examines combinations of speakers will be too slow because \(N\) is up to \(2\times 10^5\).
Algorithm
- Initialize the sum
totalto \(0\). - For each of the \(N\) speakers, perform the following:
- Calculate the distance
distanceas \(|X_i-P|\). - If
distanceis \(0\), ignore that speaker. - Otherwise, add \(\frac{V_i}{\mathrm{distance}}\) to
total.
- Calculate the distance
- Output
totalwith a sufficient number of decimal places.
Even if all speakers are at the same coordinate as the measurement point, total remains its initial value of \(0\), so it is handled correctly.
Complexity
- Time complexity: \(O(N)\)
- Space complexity: \(O(N)\)
This is because the input is read all at once usingsys.stdin.read().split(). The extra space required for computing the sum itself is \(O(1)\).
Implementation Points
By using
abs(X - P), the correct distance can be calculated regardless of whether the speaker is to the left or right of the measurement point.If the distance is \(0\), make sure to exclude it before performing division.
Since the answer is not necessarily an integer, manage
totalas a floating-point number.To satisfy precision requirements, print with a sufficient number of digits, such as
print(f"{total:.20f}").Source Code
def main():
import sys
input_data = sys.stdin.read().split()
idx = 0
N = int(input_data[idx])
idx += 1
P = int(input_data[idx])
idx += 1
total = 0.0
for _ in range(N):
X = int(input_data[idx])
idx += 1
V = int(input_data[idx])
idx += 1
distance = abs(X - P)
if distance != 0:
total += V / distance
print(f"{total:.20f}")
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.6-sol-xhigh.
投稿日時:
最終更新: