公式

A - 気温の外れ値 / Temperature Outliers 解説 by admin

Claude 4.6 Opus (Thinking)

Overview

Given the temperatures at \(N\) observation stations, the problem asks to find the station number that recorded the temperature farthest from the mean.

Analysis

What we need to find can be broken down into the following 2 steps:

  1. Calculate the mean temperature \(\mu\) across all observation stations
  2. For each observation station \(i\), calculate \(|A_i - \mu|\) and find the station where this value is maximized

For example, consider the case where \(N = 5\) and the temperatures are 10 20 15 50 18.

  • Mean: \(\mu = (10 + 20 + 15 + 50 + 18) / 5 = 113 / 5 = 22.6\)
  • Absolute deviations at each station:
    • \(|10 - 22.6| = 12.6\)
    • \(|20 - 22.6| = 2.6\)
    • \(|15 - 22.6| = 7.6\)
    • \(|50 - 22.6| = 27.4\) ← maximum
    • \(|18 - 22.6| = 4.6\)

Therefore, the answer is observation station \(4\). Intuitively, 50°C is exceptionally high compared to the others, so it can be recognized as an “outlier.”

This problem can be solved by simply examining all stations one by one; no special algorithm is needed. Since \(N \leq 2 \times 10^5\), an \(O(N)\) scan is more than sufficient.

Algorithm

  1. Compute the sum of all temperatures and divide by \(N\) to obtain the mean \(\mu\).
  2. Initialize the variables best_idx (the index of the farthest station) and best_val (the absolute deviation at that station) with the values from the first station.
  3. For each station \(i\) from the second onward, calculate \(|A_i - \mu|\). If it is greater than best_val, update best_idx and best_val.
    • Using a strict inequality > ensures that when deviations are equal, the one found first (with the smaller station number) is retained.
  4. Finally, output best_idx + 1 (converting from 0-indexed to 1-indexed).

Complexity

  • Time complexity: \(O(N)\) — \(O(N)\) for computing the sum, \(O(N)\) for finding the maximum deviation
  • Space complexity: \(O(N)\) — for storing the input array

Implementation Notes

  • Compute the mean as a floating-point number: In Python, sum(A) / N automatically returns a float, so no special handling is needed.

  • Don’t forget the conversion to 1-indexed: Python lists are 0-indexed, but the problem numbers observation stations starting from 1, so add +1 when outputting.

  • Handling ties: By using the condition val > best_val (not >=), when multiple stations have the same deviation, the one with the smallest station number is naturally selected.

    Source Code

N = int(input())
A = list(map(int, input().split()))
mu = sum(A) / N
best_idx = 0
best_val = abs(A[0] - mu)
for i in range(1, N):
    val = abs(A[i] - mu)
    if val > best_val:
        best_val = val
        best_idx = i
print(best_idx + 1)

This editorial was generated by claude4.6opus-thinking.