公式

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

GPT 5.2 High

Overview

Find the observation point where the distance \(|A_i-\mu|\) from the mean temperature \(\mu\) across all points is maximized, and if there is a tie, output the smallest index.

Approach

Since the mean is \(\mu=\dfrac{\sum_{j=1}^{N}A_j}{N}\), we can compute \(|A_i-\mu|\) for each point and find the maximum.

However, if we naively compute \(\mu\) using floating point (double) and compare, errors can cause issues such as: - Distances that are actually equal appear to have different magnitudes - Values that should be equal differ slightly, breaking comparisons

These can lead to WA (especially dangerous given the “smallest index in case of ties” condition).

Therefore, we use the idea of comparing fractions as fractions without converting them. [ |A_i-\mu| = \left|A_i-\frac{S}{N}\right| = \left|\frac{A_iN-S}{N}\right| ] (where \(S=\sum A_j\))

Here, since \(N\) is a positive constant common to all \(i\), for comparison purposes we can ignore the denominator \(N\) and simply compare [ |A_iN-S| ] This allows us to compare exactly using only integer arithmetic.

Example: When \(N=3, A=[0,1,2]\), we have \(S=3, \mu=1\) - \(i=1\): \(|0\cdot 3-3|=3\) (distance \(=1\)) - \(i=2\): \(|1\cdot 3-3|=0\) - \(i=3\): \(|2\cdot 3-3|=3\) (distance \(=1\))

The maximum is tied between \(i=1,3\), so we output the smallest index \(1\).

Algorithm

  1. Read input and compute the sum \(S\) of array \(A\).
  2. For each \(i=1..N\), compute the value [ v_i = |A_iN-S| ]
  3. Keep track of the \(i\) that maximizes \(v_i\) (by not updating on ties, the smallest index is preserved).
  4. Output the final \(i\).

Complexity

  • Time complexity: \(O(N)\) (sum computation + one pass)
  • Space complexity: \(O(N)\) (to store array \(A\))

Implementation Notes

  • Avoid floating point arithmetic; converting the comparison target to \(|A_iN-S|\) and comparing using only integers is safe.

  • To satisfy “smallest index in case of ties,” use only > as the update condition and do not update on == (this preserves the earlier, smaller index found first).

  • Since \(N \le 2\times 10^5\), in Python using sys.stdin.buffer.read() speeds up input.

    Source Code

import sys

def main():
    data = sys.stdin.buffer.read().split()
    if not data:
        return
    N = int(data[0])
    A = list(map(int, data[1:1+N]))
    S = sum(A)

    best_idx = 1
    best_val = -1
    for i, a in enumerate(A, 1):
        v = abs(a * N - S)
        if v > best_val:
            best_val = v
            best_idx = i
    print(best_idx)

if __name__ == "__main__":
    main()

This editorial was generated by gpt-5.2-high.