A - 気温の外れ値 / Temperature Outliers 解説 by admin
Qwen3-Coder-480BOverview
Given temperature data, find the observation station number whose absolute difference from the mean is the largest. If there are multiple such stations, choose the one with the smallest number.
Analysis
In this problem, we need to compute the absolute difference \(|A_i - \mu|\) between each observation station’s temperature \(A_i\) and the overall mean \(\mu\), then find the station number corresponding to the maximum value.
A straightforward approach is to first compute the mean, then calculate the absolute difference for all observation stations and select the maximum. This method does not involve any particularly difficult operations and can be solved efficiently.
An important note is that the mean \(\mu\) is not necessarily an integer, so it must be handled as a floating-point number (float). Additionally, if multiple observation stations share the same maximum difference, we need to select the one with the smallest number, so we iterate from the beginning and record the index only when the maximum value is strictly updated.
Algorithm
- Read the number of observation stations \(N\) and the temperature list \(A\) from the input.
- Compute the mean \(\mu\):
$\( \mu = \frac{\sum_{i=1}^{N} A_i}{N} \)$ - For each observation station \(i\), compute \(|A_i - \mu|\), and update the index if it is larger than the current maximum.
- Output the station number (1-indexed) with the largest difference.
Complexity
- Time complexity: \(O(N)\)
(\(O(N)\) for computing the mean, \(O(N)\) for finding the maximum difference) - Space complexity: \(O(N)\)
(for the array \(A\) that stores the input data)
Implementation Notes
The mean should be computed as a floating-point number, e.g.,
sum(a) / n.When comparing absolute differences, only adopt the first maximum value found (to ensure the station with the smallest number is selected).
Indices are 0-based, but the output is 1-based, so +1 must be added to the resulting index.
Source Code
n = int(input())
a = list(map(int, input().split()))
mean = sum(a) / n
max_diff = -1
result_index = -1
for i in range(n):
diff = abs(a[i] - mean)
if diff > max_diff:
max_diff = diff
result_index = i + 1
print(result_index)
This editorial was generated by qwen3-coder-480b.
投稿日時:
最終更新: