A - 真夏の気温差 / Midsummer Temperature Difference Editorial by admin
gpt-6-astra-mediumOverview
Calculate the difference between the maximum and minimum temperatures for each day, and output a string consisting of L if the difference is at least \(K\) degrees, and S otherwise.
Analysis
The temperature difference on the \(i\)-th day is \(A_i - B_i\). Whether that day is a “day with a large temperature difference” can be determined by the following conditions alone:
- If \(A_i - B_i \geq K\), then
L - If \(A_i - B_i < K\), then
S
Since the evaluation for each day is independent of the temperatures of other days, it is sufficient to evaluate them in the order they are given. A straightforward approach checking each day once is well within the time limit, and no special optimization is required.
One point to be careful about is that the condition is “at least \(K\) degrees”. If you use A - B > K, a day with a temperature difference of exactly \(K\) degrees will be mistakenly evaluated as S.
For example, if \(K = 10\) and the daily temperatures are as follows, the output is LSL.
| Day | Maximum Temperature | Minimum Temperature | Temperature Difference | Result |
|---|---|---|---|---|
| \(1\) | \(35\) | \(25\) | \(10\) | L |
| \(2\) | \(32\) | \(26\) | \(6\) | S |
| \(3\) | \(38\) | \(24\) | \(14\) | L |
Algorithm
- Read the number of days \(N\) and the threshold temperature difference \(K\).
- Prepare an empty list
outto store the results. - For each day, perform the following in the order of input:
- Read the maximum temperature \(A\) and minimum temperature \(B\).
- If \(A - B \geq K\), append
Ltoout; otherwise, appendS.
- Concatenate the characters in
outwithout separators and print the result.
By appending characters according to the definition for each day while keeping the original input order, we obtain the desired string of length \(N\).
Complexity
- Time Complexity: \(O(N)\)
We evaluate each day once and concatenate \(N\) characters at the end. - Space Complexity: \(O(N)\)
We store the input data and the \(N\) evaluation results.
Implementation Points
Since the constraints guarantee \(B_i \leq A_i\), the temperature difference can be computed directly as
A - B.out.append('L' if A - B >= K else 'S')appends the character corresponding to the condition.Using
''.join(out)allows us to concatenate characters without spaces between them.In the code, all inputs are read at once using
sys.stdin.read().split(). After \(N\) and \(K\), we advanceidxby \(2\) at a time since there are \(2\) integers for each day.Source Code
import sys
def main():
data = sys.stdin.read().split()
idx = 0
N = int(data[idx]); idx += 1
K = int(data[idx]); idx += 1
out = []
for _ in range(N):
A = int(data[idx]); B = int(data[idx + 1]); idx += 2
out.append('L' if A - B >= K else 'S')
sys.stdout.write(''.join(out) + '\n')
main()
This editorial was generated by gpt-6-astra-medium.