A - 朗読の評価 / Evaluation of Reading Aloud Editorial by admin
swe-2-highSolution
For each student \(i\), check whether the condition “\(A_i \geq X\) and \(B_i \leq Y\)” holds. If it does, the \(i\)-th character of the output is G; otherwise it is B.
While reading the input in order, decide each character on the spot. Since the judgment for each student is independent, there is no need to store the answers in an array; we can just evaluate each pair as it is read and append the result to the output string.
Pseudocode
s ← empty string
for i = 1 .. N:
read A_i, B_i
if A_i ≥ X and B_i ≤ Y: append 'G' to s
else: append 'B' to s
print s
Proof of correctness
Claim: The \(i\)-th character of the string \(s\) produced by the algorithm equals the evaluation (G or B) of student \(i\).
Proof: In the \(i\)-th iteration of the loop, the algorithm reads \(A_i\) and \(B_i\) from the input. By the problem statement, student \(i\) receives G exactly when “\(A_i \geq X\) and \(B_i \leq Y\)”, and B otherwise. The algorithm checks precisely this condition and appends G if it is true and B if it is false. Hence the \(i\)-th character of \(s\) equals student \(i\)’s evaluation. This holds for every \(i = 1, \dots, N\), and since characters are appended in order from student 1 to student \(N\), the output is exactly “the string of length \(N\) listing the evaluations in order from student 1”. \(\blacksquare\)
Boundary cases (\(A_i = X\) exactly, \(B_i = Y\) exactly) are also handled correctly, because the comparison operators >= and <= implement “at least” and “at most” exactly as defined. Each student’s evaluation is uniquely determined, so no tie-breaking exists.
Complexity
- Time: \(O(N)\), including reading the input.
- Space: \(O(N)\) for C++ and \(O(N)\) for Python, including stored input and output arrays or strings.