Official

A - 布団の組 / Futon Pairs Editorial by admin Editorial for printing

swe-2-high

Solution

On day \(i\), the total number of guests is \(A_i + B_i\). Since futons are laid out in pairs of two people, they form complete pairs exactly when \(A_i + B_i\) is even, and one person is left over exactly when it is odd. Therefore, for each day we output o if \((A_i + B_i) \bmod 2 = 0\) and x if it is \(1\).

In the implementation, we read \(N\), then read each pair \(A_i, B_i\) in order, check the parity of the sum, append o or x to the answer string, and finally print the whole string.

Proof of Correctness

Claim: The \(i\)-th character of the output is o when \(A_i + B_i\) is even and x when it is odd.

Proof: In the \(i\)-th iteration of the loop, \(A_i\) and \(B_i\) are read from the input. The program computes \((A_i + B_i) \bmod 2\) and appends o to the answer string if the result is \(0\), otherwise x. Since the parity of an integer is uniquely determined by its residue modulo 2, \(A_i + B_i\) is even if and only if o is appended, and odd if and only if x is appended. This holds for every \(i = 1, \dots, N\), so the output string coincides with the definition in the statement.

Whether the guests form complete pairs or one person is left over is determined exclusively by parity, so there is no ambiguity and no tie-breaking to consider.

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.

posted:
last update: