Official

A - 的当てゲーム / Target Shooting Game Editorial by admin

GPT 5.2 High

Overview

Each ball flies a distance equal to its weight \(D_i\), so the problem asks us to count the number of balls whose distance falls within the target’s range \([L-W,\, L+W]\).

Analysis

  • Since a ball with weight \(D\) flies “exactly distance \(D\)”, we can consider the landing position = the weight.
  • The condition for hitting the target is “the landing distance is at least \(L-W\) and at most \(L+W\)”, so for each \(D_i\) we just need to check whether
    • \(L-W \le D_i \le L+W\) is satisfied.
  • Since \(N \le 2\times 10^5\), a straightforward brute-force approach checking each ball once in \(O(N)\) is more than sufficient.
  • Alternatively, one could sort and use binary search (\(O(N\log N)\)), but that is unnecessary here. Simple condition checking gives us the answer.

Example: - When \(L=10,\ W=2\), the target’s range is \([8,12]\). - If \(D=[3,8,10,13,12]\), the balls that hit are \(8,10,12\), giving 3.

Algorithm

  1. Compute the lower bound \(low = L-W\) and upper bound \(high = L+W\) of the distances that hit the target.
  2. For each ball’s weight \(D_i\), increment the count if \(low \le D_i \le high\).
  3. Output the count.

Complexity

  • Time complexity: \(O(N)\) (checking each \(D_i\) once)
  • Space complexity: \(O(1)\) (additional space excluding the input array)

Implementation Notes

  • The check includes both endpoints (“at least” and “at most”), so the condition is low <= x <= high.

  • \(L, W, D_i\) can be up to \(10^9\), but Python integers handle this without overflow concerns.

  • Since the input can be large, using sys.stdin.readline is recommended for safety.

    Source Code

import sys

def main():
    input = sys.stdin.readline
    N, L, W = map(int, input().split())
    D = list(map(int, input().split()))
    low = L - W
    high = L + W
    ans = sum(1 for x in D if low <= x <= high)
    print(ans)

if __name__ == "__main__":
    main()

This editorial was generated by gpt-5.2-high.

posted:
last update: