Official

A - ボウリングの記録 / Bowling Record Editorial by admin Editorial for printing

gpt-6-astra-medium

Overview

This problem asks us to convert the number of pins knocked down in each throw into a single character according to the specified rules, and output them concatenated in the order of the throws.

Analysis

The symbol for each throw is determined solely by the number of pins knocked down in that throw. There is no need to consider the throws before or after it.

We just need to convert \(A_i\) as follows:

  • If \(A_i = 10\), then X
  • If \(A_i = 0\), then -
  • Otherwise, the digit representing \(A_i\)

For example, if the number of pins knocked down is \(10, 0, 3, 9, 10\), the output is X-39X.

In this problem, a naive approach of processing all throws in order easily runs well within the time limit. However, simply converting the numbers directly to strings would turn \(10\) into 10 and \(0\) into 0, which is incorrect. We handle these \(2\) cases specially using conditional branches.

Algorithm

  1. Read the number of throws \(N\) and the number of pins for each throw.
  2. Initialize an empty list ans to store the characters.
  3. Process each throw from first to last:
    • If the number of pins is \(10\), append 'X' to ans.
    • If the number of pins is \(0\), append '-' to ans.
    • Otherwise, convert the number of pins to a string using str(a) and append it.
  4. Concatenate the characters without delimiters using ''.join(ans) and output the result.

Since a single character according to the rules is appended in the order of the throws for each throw, the resulting string will be the desired record of length \(N\).

Complexity

  • Time Complexity: \(O(N)\)
    Both converting each throw and joining the final string take \(O(N)\) time.
  • Space Complexity: \(O(N)\)
    Uses memory for the input data and the list storing the \(N\) characters.

Implementation Notes

  • Branch into cases using if a == 10, elif a == 0, and else. From the constraints, the value in the else case is always between \(1\) and \(9\) inclusive, so str(a) is guaranteed to be \(1\) character.

  • Use ''.join(ans) so that no spaces are inserted between the output characters.

  • In the sample solution, the entire input is split using split(), so data[0] corresponds to \(N\), and data[1 + i] corresponds to the number of pins in the \(i+1\)-th throw.

    Source Code

import sys

def main():
    data = sys.stdin.read().split()
    n = int(data[0])
    ans = []
    for i in range(n):
        a = int(data[1 + i])
        if a == 10:
            ans.append('X')
        elif a == 0:
            ans.append('-')
        else:
            ans.append(str(a))
    sys.stdout.write(''.join(ans) + '\n')

main()

This editorial was generated by gpt-6-astra-medium.

posted:
last update: