Official

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

DeepSeek V3

Overview

Given temperature data for each observation point, find the number of the point with the largest absolute difference from the mean. If there are multiple such points, output the smallest number.

Analysis

In this problem, we first need to calculate the mean temperature \(\mu\) across all observation points. The mean is obtained by dividing the total sum of all temperatures by the number of observation points \(N\). Then, for each observation point, we calculate \(|A_i - \mu|\) and find the point where this value is maximized.

An important condition is that when the distances from the mean are equal, we must select the one with the “smallest number.” Also, while the temperature values are integers, the mean can be a decimal, so floating-point arithmetic is required.

Algorithm

  1. Read the number of observation points \(N\) and the temperature list \(A\) from input
  2. Calculate the total sum of all temperatures and compute the mean \(\mu = \frac{\text{sum}(A)}{N}\)
  3. Loop through each observation point:
    • Calculate \(|A_i - \mu|\)
    • If it is greater than the current maximum difference, update the maximum difference and the result index
    • If it equals the current maximum difference and the index is smaller than the current result, update the result index
  4. Add 1 to the final obtained index (0-indexed) and output it (converting to 1-indexed)

Complexity

  • Time complexity: \(O(N)\)
    • \(O(N)\) for computing the sum of temperatures
    • \(O(N)\) for computing and comparing the difference at each observation point
  • Space complexity: \(O(N)\)
    • Array for storing the temperature data

Implementation Notes

  • Floating-point numbers must be used when calculating the mean

  • When comparing indices, only compare the numbers when the difference values are equal

  • Don’t forget to convert from 0-indexed to 1-indexed (add +1 to the result)

  • Set initial values of maximum difference to -1 and result index to -1, ensuring they are updated at the first element

    Source Code

def main():
    import sys
    data = sys.stdin.read().split()
    n = int(data[0])
    A = list(map(int, data[1:1+n]))
    
    total = sum(A)
    mu = total / n
    
    max_diff = -1
    result_index = -1
    
    for i in range(n):
        diff = abs(A[i] - mu)
        if diff > max_diff:
            max_diff = diff
            result_index = i
        elif diff == max_diff:
            if i < result_index:
                result_index = i
                
    print(result_index + 1)

if __name__ == "__main__":
    main()

This editorial was generated by deepseekv3.