Official

A - 真夏の気温差 / Midsummer Temperature Difference Editorial by admin

gpt-6-astra-medium

Overview

For each day, find the difference between the maximum and minimum temperatures, and output L if the difference is at least \(K\) degrees, and S otherwise, concatenated in order.

Analysis

The temperature difference on the \(i\)-th day is \(A_i-B_i\), so the condition for a day with a large temperature difference is:

\(A_i-B_i \geq K\)

By adding \(B_i\) to both sides, this condition can also be written as:

\(B_i+K \leq A_i\)

The reference solution uses this latter condition.

For example, when \(K=10\), if the maximum temperature is \(35\) degrees and the minimum temperature is \(25\) degrees, the temperature difference is exactly \(10\) degrees, so the output is L. If the minimum temperature is \(26\) degrees, the difference is \(9\) degrees, so the output is S.

Since the judgment for each day is independent of the other days, it is sufficient to process each day one by one in the order it is given in the input. This straightforward approach easily runs within the time limit. Note that the condition specifies “at least \(K\) degrees”, so classifying a difference of exactly \(K\) degrees as S will result in a Wrong Answer.

Algorithm

  1. Read \(N\) and \(K\), and initialize an empty string s to store the answer.
  2. For each day from day \(1\) to day \(N\), repeat the following:
    • Read the maximum temperature \(a\) and the minimum temperature \(b\).
    • If \(b+K \leq a\), append L to the end of s.
    • Otherwise, append S to the end of s.
  3. Output the completed string s.

Since the character corresponding to the condition for each day is appended in the input order, the resulting string will be the required answer.

Complexity

  • Time complexity: \(O(N)\)
    We perform a check and append a character once for each day.
  • Space complexity: \(O(N)\)
    We store the answer string of length \(N\). There is no need to store all the temperatures for every day.

Implementation Points

  • Include the equal sign in the comparison: b + k <= a.

  • In the code, (b + k <= a) ? 'L' : 'S' is an expression that evaluates to 'L' if the condition is satisfied, and 'S' otherwise.

  • Output the answer as a single string of length \(N\) on one line without spaces between the characters.

    Source Code

// 問題文から独立に実装: 最低気温に K を足しても最高気温を超えないか
#include <iostream>
#include <string>
int main() {
    int n, k; std::cin >> n >> k;
    std::string s;
    for (int i = 0; i < n; ++i) {
        int a, b; std::cin >> a >> b;
        s += (b + k <= a) ? 'L' : 'S';
    }
    std::cout << s << std::endl;
}

This editorial was generated by gpt-6-astra-medium.