公式

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

Gemini 3.0 Flash (Thinking)

Overview

Given \(N\) temperature data points \(A_1, A_2, \ldots, A_N\), the problem asks to output the index of the data point that is farthest from the mean \(\mu\) (i.e., the one that maximizes \(|A_i - \mu|\)).

Analysis

The key point of this problem is how to avoid floating-point precision errors.

Straightforward Approach

A natural approach is to compute the mean \(\mu\) and then find the absolute difference between each data point and the mean. 1. Compute the sum of temperatures \(S = \sum A_i\). 2. Compute the mean \(\mu = S / N\). 3. For each \(i\), compute \(|A_i - \mu|\) and find the \(i\) that maximizes it.

However, floating-point numbers in programming (such as float type) are represented internally in binary, so division results can introduce small errors. Since \(N\) can be as large as \(2 \times 10^5\), these errors can cause incorrect judgments (WA) when exact comparisons are needed.

Avoiding Precision Errors

The expression we want to compare is \(|A_i - \mu|\). Let’s substitute \(\mu = S / N\) and transform it.

\[|A_i - \frac{S}{N}| = \frac{|N \cdot A_i - S|}{N}\]

We want to find the \(i\) that maximizes this value. Since the denominator \(N\) is a positive integer common to all observation points, maximizing the entire expression is equivalent to maximizing the numerator \(|N \cdot A_i - S|\).

In this expression, all values involved are integers (\(N, A_i, S\)), so no errors occur during computation, and we can perform exact comparisons.

Algorithm

  1. Compute the sum \(S\) of the input temperatures.
  2. Initialize a variable max_diff to hold “the maximum difference so far” with a very small value (or \(-1\)).
  3. For each observation point \(i = 1, \ldots, N\), repeat the following:
    • Compute the criterion value \(D = |N \cdot A_i - S|\).
    • If \(D\) is strictly greater than max_diff:
      • Update max_diff with \(D\).
      • Update the answer index ans_idx with \(i\).
  4. Output the final ans_idx.

※ By not updating when “\(D\) equals max_diff”, we naturally satisfy the condition stated in the problem: “if there are multiple such points, output the one with the smallest index.”

Complexity

  • Time Complexity: \(O(N)\)
    • Computing the sum of all data takes \(O(N)\), and checking each data point once takes \(O(N)\), so the overall complexity is \(O(N)\). Since \(N \le 2 \times 10^5\), this is sufficiently fast.
  • Space Complexity: \(O(N)\)
    • \(O(N)\) memory is used to store the \(N\) input data points in a list.

Implementation Notes

  • Integer Comparison: As described above, by using abs(n * a[i] - s) as the comparison target, we perform the judgment without using float types.

  • Tiebreaking: When multiple points share the same maximum value, we prioritize the first one encountered (= the one with the smallest index), so we use the > comparison operator and do not use >=.

  • I/O Efficiency: Since \(N\) can be large, in Python it is faster to read all input at once using sys.stdin.read().split() or similar methods.

    Source Code

import sys

def main():
    # 入力をすべて読み込む
    input_data = sys.stdin.read().split()
    if not input_data:
        return
    
    # Nの取得
    n = int(input_data[0])
    # A_iのリストを取得
    a = list(map(int, input_data[1:]))
    
    # 全観測地点の気温の合計を求める(平均値 mu = s / n)
    s = sum(a)
    
    # |A_i - mu| が最大となる地点を探す
    # |A_i - s/n| を最大化することは、|n * A_i - s| を最大化することと同義
    # これにより浮動小数点数の精度問題を回避できる
    max_diff = -1
    ans_idx = 1
    
    for i in range(n):
        # |n * A_i - s| を計算
        current_diff = abs(n * a[i] - s)
        
        # 最大値を更新した場合のみ番号を更新する
        # (同じ値の場合は番号が小さい方を優先するため、より大きい場合のみ更新)
        if current_diff > max_diff:
            max_diff = current_diff
            ans_idx = i + 1
            
    # 結果を出力
    print(ans_idx)

if __name__ == "__main__":
    main()

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