A - 真夏の気温差 / Midsummer Temperature Difference 解説 by admin
swe-2-highEditorial
Problem Summary
We are given the highest temperature \(A_i\) and the lowest temperature \(B_i\) for each of \(N\) days. For each day, if \(A_i - B_i \geq K\) we output L, otherwise S, and we print the resulting string of length \(N\) formed by concatenating these characters in order from day 1.
Algorithm
For each day \(i\), compute the temperature difference \(A_i - B_i\) and check whether it is at least \(K\). Append the resulting character to the answer string in order.
- If \(A_i - B_i \geq K\):
L - If \(A_i - B_i < K\):
S
Since each day’s data can be processed as it is read, there is no need to store the entire input in an array.
Proof of Correctness
Claim: The \(i\)-th character of the output is L when \(A_i - B_i \geq K\), and S otherwise.
Proof: For each \(i\) (\(1 \leq i \leq N\)), the program reads \(A_i, B_i\) from the input and directly evaluates the condition \(A_i - B_i \geq K\). It appends L as the \(i\)-th character if the condition holds, and S otherwise. This exactly matches the problem’s definition of a “day with a large temperature difference” (a day whose difference between the highest and lowest temperatures is at least \(K\) degrees). Since characters are appended in the order \(i = 1, 2, \ldots, N\), the output is a string of length \(N\) ordered from day 1 onward. □
Boundary condition: a day whose difference is exactly \(K\) counts as “at least \(K\) degrees” and yields L. This is correctly handled by the condition A - B >= K. No tie-breaking exists in this problem.
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.