Official

B - 連続正解 / Consecutive Correct Answers Editorial by admin Editorial for printing

gpt-6-astra-medium

Overview

Output o for problems up to the first mistake, and output x for the first mistaken problem and all subsequent problems. By finding the position of the first \(0\), we can construct the answer all at once.

Analysis

Points are awarded only as long as problems are answered correctly without any mistakes from the very first problem.

Therefore, simply replacing each \(A_i\) by “if \(1\), then o; if \(0\), then x” is not correct. Once a mistake is made, subsequent problems must be marked with x even if they are answered correctly.

For example, consider the following results:

1 1 0 1 0

Points are awarded for the first \(2\) problems, but because a mistake was made on the \(3\)-rd problem, no points are awarded from that point onward. Therefore, the answer is the following string:

ooxxx

Here, since all elements before the first \(0\) are \(1\), it is sufficient to know only the position of the first \(0\). If there are no \(0\)s at all, points are awarded for all problems.

Note that sequentially checking from the beginning while keeping track of whether a mistake has already been made is also fast enough. In this problem, properly handling the problems after the first mistake is more important than speed optimization.

Algorithm

  1. Read \(N\) and the array \(A\).
  2. Let first_miss be the \(0\)-based index of the first \(0\).
    • If a \(0\) exists, find it using a.index(0).
    • If no \(0\) exists, set first_miss = n.
  3. Concatenate and print the following strings:
    • A string consisting of first_miss occurrences of o
    • A string consisting of \(N - \text{first\_miss}\) occurrences of x

The index of the first \(0\) equals the number of correct answers before it. Thus, this method correctly represents the problems for which points were awarded and those that followed.

Complexity

  • Time complexity: \(O(N)\)
  • Space complexity: \(O(N)\)

Checking for the presence of \(0\), finding the position of the first \(0\), and generating the output string all take at most \(O(N)\) time. Furthermore, \(O(N)\) space is used to store the input array and the output string.

Implementation Notes

  • In Python, a.index(0) returns the index of the first \(0\). However, it raises an error if \(0\) is not present, so check with 0 in a first.

  • Multiplying a string by an integer repeats the character a specified number of times. For example, 'o' * 3 is ooo.

  • If a mistake is made on the first problem, first_miss = 0, resulting in all xs. If all problems are answered correctly, first_miss = n, resulting in all os.

    Source Code

# 問題文から独立に実装: 最初の 0 の位置までが o
n = int(input())
a = list(map(int, input().split()))
first_miss = a.index(0) if 0 in a else n
print('o' * first_miss + 'x' * (n - first_miss))

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

posted:
last update: