C - エアコンの温度調整 / Air Conditioner Temperature Adjustment Editorial by admin
Qwen3-Coder-480BOverview
Find the minimum number of cooling packs needed to bring each room’s temperature to or below the target temperature.
Analysis
If the current temperature \(T_i\) of each room exceeds the target temperature \(M\), the difference \(T_i - M\) needs to be eliminated using cooling packs. Since one cooling pack can lower the temperature by \(D\) degrees, the required number is \(\left\lceil \frac{T_i - M}{D} \right\rceil\) (rounded up to the nearest integer).
For example, if a room’s temperature is \(10\) degrees, the target is \(3\) degrees, and one pack can lower the temperature by \(2\) degrees, the required cooling is \(7\) degrees, so \(\left\lceil 7 / 2 \right\rceil = 4\) packs are needed.
A naive approach of simulating the temperature reduction one pack at a time would take too long when \(D\) is very large, resulting in TLE (Time Limit Exceeded). Instead, we can solve this efficiently by mathematically computing the required number of packs directly.
Algorithm
- For each room’s temperature \(T_i\), if \(T_i > M\), compute the required cooling amount \(T_i - M\).
- Calculate the required number of cooling packs as \(\left\lceil \frac{T_i - M}{D} \right\rceil\) and add it to the total.
- The final total is the answer.
Complexity
- Time complexity: \(O(N)\)
- Space complexity: \(O(1)\) (excluding input)
Implementation Notes
- Use
math.ceil()to perform the ceiling operation. - If the temperature is already at or below \(M\), it can be ignored.
- Since the input range is large, an efficient calculation is necessary (simulation is not feasible).
## Source Code
```python
import math
# 入力の読み込み
N, M, D = map(int, input().split())
T = list(map(int, input().split()))
# 必要な冷却パックの合計個数
total_packs = 0
for temp in T:
if temp > M:
# 温度をM以下にするために必要な冷却量
need_cooling = temp - M
# 必要な冷却パックの個数(切り上げ)
packs_needed = math.ceil(need_cooling / D)
total_packs += packs_needed
# 結果の出力
print(total_packs)
This editorial was generated by qwen3-coder-480b.