公式

A - エアコンの温度調整 / Air Conditioner Temperature Adjustment 解説 by admin

GPT 5.2 High

Overview

This problem asks you to compute the total number of operations needed to lower each room’s temperature \(T_i\) by \(0.1\) degrees at a time, only for the amount exceeding \(37\) degrees.

Analysis

Since each operation lowers the temperature by \(0.1\) degrees, when a room’s temperature exceeds \(37\) degrees, the required number of operations is “the excess divided by \(0.1\).” For example, if the temperature is \(37.2\) degrees, it needs to be lowered by \(0.2\) degrees, so \(0.2 / 0.1 = 2\) operations.

An important concern here is handling floating-point numbers. If you naively compute with float, internal representation errors can cause issues such as: - \(37.1 - 37.0 = 0.1\) should hold, but it becomes \(0.099999...\)

This can lead to WA due to incorrect rounding decisions.

In this problem, the input is guaranteed to have “at most one decimal place,” so it is safe to multiply the temperature by 10 and work with integers (in units of 0.1 degrees). - \(37.2 \rightarrow 372\) - \(36.9 \rightarrow 369\)

Then, the required number of operations is simply: - If \(v > 370\), add \(v - 370\) - Otherwise, add \(0\)

and sum them all up.

While a simulation approach (looping to subtract \(0.1\) degrees one at a time) might also work, the essence of the problem is just “counting the difference,” so direct computation is simpler and more reliable.

Algorithm

  1. Read each temperature \(T_i\) as a string and convert it to an integer \(v\) in units of \(0.1\) degrees (= temperature \(\times 10\)).
    • Example: "40.3" becomes \(403\)
  2. Treat the threshold \(37.0\) degrees similarly as \(370\).
  3. For each room, if \(v > 370\), add the excess \(v - 370\) to the answer.
  4. Output the total.

Written as a formula, the number of operations for room \(i\) is: $\( \max(0, 10T_i - 370) \)\( (where \)10T_i$ is the integer-converted value).

Complexity

  • Time complexity: \(O(N)\) (each room is processed exactly once)
  • Space complexity: \(O(1)\) (only a constant number of variables such as the running total. Even implementations that read the entire input only add a constant amount of extra space)

Implementation Notes

  • Avoid floating-point arithmetic: Since the input has at most one decimal place, converting directly from the string to “the integer multiplied by 10” eliminates rounding errors.

    • The to_tenths function in the code converts "a.b" to a*10 + b for safe processing.
  • The threshold \(37.0\) degrees is compared as \(370\) (in units of 0.1 degrees), and only the excess is accumulated with ans += v - 370.

  • Since the input can have up to \(2 \times 10^5\) entries, reading all at once with sys.stdin.buffer.read() is faster.

    Source Code

import sys

def to_tenths(s: str) -> int:
    if '.' in s:
        a, b = s.split('.', 1)
        return int(a) * 10 + int(b[0])
    return int(s) * 10

def main():
    data = sys.stdin.buffer.read().split()
    if not data:
        return
    n = int(data[0])
    temps = data[1:1+n]
    ans = 0
    for t in temps:
        v = to_tenths(t.decode())
        if v > 370:
            ans += v - 370
    print(ans)

if __name__ == "__main__":
    main()

This editorial was generated by gpt-5.2-high.

投稿日時:
最終更新: