A - 真夏の気温差 / Midsummer Temperature Difference Editorial by admin Editorial for printing
gpt-6-astra-mediumOverview
For each day, find the difference between the maximum and minimum temperatures, and output L if the difference is at least \(K\) degrees, or S otherwise, concatenated in order.
Analysis
The temperature difference on the \(i\)-th day is \(A_i - B_i\). Whether a day has a “large temperature difference” depends solely on that day’s temperatures, so each day can be evaluated independently.
The conditions are as follows:
- If \(A_i - B_i \geq K\):
L - If \(A_i - B_i < K\):
S
For example, when \(K = 10\), if the maximum temperature is \(30\) degrees and the minimum temperature is \(20\) degrees, the temperature difference is \(10\) degrees, resulting in L. If the minimum temperature is \(21\) degrees, the temperature difference is \(9\) degrees, resulting in S.
In this problem, a naive approach of checking all days sequentially is easily fast enough. However, note that “at least \(K\) degrees” includes the case where the difference is exactly \(K\) degrees; using the condition a - b > k will lead to a Wrong Answer. Be sure to check with a - b >= k.
Algorithm
- Read \(N\) and \(K\).
- Initialize an empty list
resto store the resulting characters. - For each day from day \(1\) to day \(N\), perform the following:
- Read the maximum temperature \(A_i\) and minimum temperature \(B_i\).
- If \(A_i - B_i \geq K\), append
Ltores; otherwise, appendS.
- Concatenate the characters in
reswithout separators and output the result.
Since the results are appended in the order of input, the resulting string will contain the correct evaluation for each day, ordered from day \(1\) to day \(N\).
Complexity
- Time complexity: \(O(N)\)
It takes \(O(1)\) to evaluate each day, and \(O(N)\) to concatenate the final string. - Space complexity: \(O(N)\)
The results are stored in the listres.
Implementation Notes
You can determine the character for each day using the conditional expression
'L' if a - b >= k else 'S'.Since \(B_i \leq A_i\) is guaranteed, the temperature difference can simply be calculated as
a - b.Using
print("".join(res))allows you to print the string of length \(N\) on a single line without spaces between characters.Source Code
# 問題文から独立に実装
n, k = map(int, input().split())
res = []
for _ in range(n):
a, b = map(int, input().split())
res.append('L' if a - b >= k else 'S')
print("".join(res))
This editorial was generated by gpt-6-astra-medium.
posted:
last update: