Official

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

gemini-3-flash-preview

Overview

This problem asks you to determine the total number of operations needed to reduce the temperature \(T_i\) of all \(N\) rooms to \(37.0\) degrees or below, where each operation decreases the temperature by \(0.1\) degrees.

Analysis

The most important point to be careful about when solving this problem is floating-point precision errors.

Since computers internally represent decimal numbers in binary, the value 0.1 cannot be represented exactly, and slight errors can occur. For example, computing 37.1 - 37.0 may not yield exactly 0.1, but instead a value like 0.09999999999999432. If you perform division or comparisons with such errors included, it can cause the answer to be off by \(1\) (resulting in WA: Wrong Answer).

In this problem, all temperatures \(T_i\) are given “to exactly one decimal place.” Therefore, by multiplying all values by 10 and treating them as integers, we can completely eliminate precision errors.

  • Target temperature: \(37.0 \times 10 = 370\)
  • Change per operation: \(0.1 \times 10 = 1\)
  • Each room’s temperature: \(T_i \times 10\)

With this conversion, if a room’s temperature \(T_i\) is higher than \(37.0\) degrees, the required number of operations can be calculated exactly as integer subtraction: (T_i multiplied by 10 as an integer) - 370.

Algorithm

  1. Initialize a variable total_operations representing the total number of operations to \(0\).
  2. For each room’s temperature \(T_i\), repeat the following process:
    • Let \(T_{scaled}\) be the value obtained by multiplying \(T_i\) by 10 and converting to an integer.
    • If \(T_{scaled} > 370\), add the difference T_scaled - 370 to total_operations.
  3. Output the final total_operations.

Complexity

  • Time complexity: \(O(N)\)
    • For \(N\) rooms, we check each room’s temperature once, so the computation finishes in time proportional to the number of rooms. Since \(N \leq 2 \times 10^5\), this is sufficiently fast.
  • Space complexity: \(O(N)\)
    • If all input temperatures are stored in a list or similar structure, memory proportional to \(N\) is used.

Implementation Notes

  • Converting decimals to integers: In Python, using int(float(s) * 10) may cause 37.1 to become 370 incorrectly due to precision errors. To prevent this, it is safer to use round(float(s) * 10) to round to the nearest integer before converting to an integer type.

  • Fast input: Since \(N\) can be as large as \(2 \times 10^5\), using sys.stdin.read().split() or similar methods to read all input at once can reduce execution time.

    Source Code

import sys

def solve():
    # 標準入力からすべてのデータを読み込み、空白(スペースや改行)で分割します。
    # これにより、N と各温度 Ti を効率的に取得できます。
    input_data = sys.stdin.read().split()
    
    if not input_data:
        return
    
    # 1番目の要素は部屋の数 N です。
    n = int(input_data[0])
    
    # 必要な操作の合計回数を保持する変数です。
    total_operations = 0
    
    # 2番目以降の要素(各部屋の温度 Ti)を順番に処理します。
    for i in range(1, n + 1):
        # 温度は小数第1位まで与えられるため、浮動小数点の精度問題を避けるために
        # 10倍して整数として扱います。
        # float() で変換した後、10倍し、round() で丸めることで正確な整数値を得ます。
        # 例: 37.1度 -> 371, 37.0度 -> 370
        t_scaled = int(round(float(input_data[i]) * 10))
        
        # 37度(整数スケールで370)より高い場合、その差分が操作回数となります。
        if t_scaled > 370:
            total_operations += (t_scaled - 370)
            
    # 合計回数を整数で出力します。
    print(total_operations)

if __name__ == '__main__':
    solve()

This editorial was generated by gemini-3-flash-preview.

posted:
last update: