A - 温度センサーの点検 / Inspection of Temperature Sensors 解説 by admin
Qwen3-Coder-480BOverview
Given measurement values from temperature sensors arranged in a line, calculate the “local temperature indicator” for each sensor and find the maximum value.
Analysis
In this problem, we need to calculate the local temperature indicator for each sensor, which is “the sum of the measurement values of the sensor itself and its adjacent sensors,” and find the maximum value among them.
It is important to note that the calculation method differs depending on the position of the sensor: - Edge sensors (first and last) have a neighbor on only one side, so the indicator is the sum of 2 values. - All other sensors have neighbors on both sides, so the indicator is the sum of 3 values (including the previous and next sensors).
For example, if the input is 5 and the measurement values are [10, -5, 3, 2, 8]:
- Sensor 1: \(10 + (-5) = 5\)
- Sensor 2: \(10 + (-5) + 3 = 8\)
- Sensor 3: \(-5 + 3 + 2 = 0\)
- Sensor 4: \(3 + 2 + 8 = 13\)
- Sensor 5: \(2 + 8 = 10\)
The maximum among these is \(13\).
A straightforward approach is to compute the sum for every sensor and update the maximum value each time. Given the constraints, the number of sensors \(N\) is at most \(2 \times 10^5\), so it is fast enough as long as each sensor can be processed in constant time. This runs sufficiently fast even in languages like Python.
Algorithm
- Read the number of sensors \(N\) and the measurement value list \(A\) from input.
- Initialize a variable
max_indicatorto negative infinity to hold the maximum value. - Calculate the local temperature indicator for the first sensor (index 0) and update the maximum value.
- For the middle sensors (indices 1 to \(N-2\)), calculate the sum of 3 terms for each and update the maximum value.
- Calculate the local temperature indicator for the last sensor (index \(N-1\)) and update the maximum value.
- Output the final maximum value.
Complexity
- Time complexity: \(O(N)\)
- Space complexity: \(O(N)\)
Implementation Notes
- Pay attention to index ranges (especially for the first and last sensors).
- By updating the maximum value each time an indicator is calculated, there is no need to search for the maximum at the end.
- Using Python’s
float('-inf')makes the initial comparison straightforward.
## Source Code
```python
N = int(input())
A = list(map(int, input().split()))
max_indicator = float('-inf')
# 最初のセンサー (i=1)
M1 = A[0] + A[1]
if M1 > max_indicator:
max_indicator = M1
# 中間のセンサー (1 < i < N)
for i in range(1, N-1):
Mi = A[i-1] + A[i] + A[i+1]
if Mi > max_indicator:
max_indicator = Mi
# 最後のセンサー (i=N)
MN = A[N-2] + A[N-1]
if MN > max_indicator:
max_indicator = MN
print(max_indicator)
This editorial was generated by qwen3-coder-480b.
投稿日時:
最終更新: