A - ボウリングの記録 / Bowling Record Editorial by admin Editorial for printing
gpt-6-astra-mediumOverview
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
- Read the number of throws \(N\) and the number of pins for each throw.
- Initialize an empty list
ansto store the characters. - Process each throw from first to last:
- If the number of pins is \(10\), append
'X'toans. - If the number of pins is \(0\), append
'-'toans. - Otherwise, convert the number of pins to a string using
str(a)and append it.
- If the number of pins is \(10\), append
- 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, andelse. From the constraints, the value in theelsecase is always between \(1\) and \(9\) inclusive, sostr(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(), sodata[0]corresponds to \(N\), anddata[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: