A - 花火大会の観覧 / Watching the Fireworks Festival 解説 by admin
gpt-5.3-codexOverview
For each firework, determine whether “the distance from Takahashi’s position is within radius \(R\),” and sum up the satisfaction values \(P_i\) of the fireworks that satisfy the condition. This can be solved by examining each firework once.
Analysis
The key observation is that the determination for each firework is independent. In other words, “whether a particular firework is visible” depends only on that firework’s coordinates and Takahashi’s coordinates — no information about other fireworks is needed.
The Euclidean distance between firework \((X_i, Y_i)\) and Takahashi \((X_A, Y_A)\) is
\( \sqrt{(X_i - X_A)^2 + (Y_i - Y_A)^2} \)
If this is at most \(R\), the firework is visible.
While naively computing the square root each time can produce correct results, it tends to be inefficient and unstable for the following reasons:
- Square root computation is relatively expensive
- Floating-point errors make boundary cases (exactly distance \(R\)) unreliable
Therefore, for comparison purposes only, the square root is unnecessary. We use the following equivalence:
\( \sqrt{d^2} \le R \iff d^2 \le R^2 \)
That is, we simply check:
\( (X_i - X_A)^2 + (Y_i - Y_A)^2 \le R^2 \)
using integers only. This allows for both fast and safe determination.
Algorithm
- Read \(N, X_A, Y_A, R\) from input.
- Precompute \(r2 = R^2\).
- Initialize the total
totalto 0. - For each firework:
- Compute \(dx = X_i - X_A\), \(dy = Y_i - Y_A\)
- If \(dx^2 + dy^2 \le r2\), then
total += P_i
- Output
totalat the end.
Concrete example: When \(R=5\), \(R^2=25\). For a firework with \(dx=3, dy=4\), we get \(dx^2+dy^2=9+16=25\), so it is determined as “visible” (boundary included).
Complexity
- Time complexity: \(O(N)\)
- Space complexity: \(O(1)\)
Implementation Notes
For distance comparison, do not use square roots — compare the squares directly.
The condition is “less than or equal to,” so use
<=(fireworks exactly on the boundary are also viewable).Coordinate differences and their squares can become large, but Python’s integers have arbitrary precision, so they can be handled safely as-is.
Source Code
import sys
def main():
input = sys.stdin.readline
N, XA, YA, R = map(int, input().split())
r2 = R * R
total = 0
for _ in range(N):
x, y, p = map(int, input().split())
dx = x - XA
dy = y - YA
if dx * dx + dy * dy <= r2:
total += p
print(total)
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.3-codex.
投稿日時:
最終更新: