結果

問題 No.1134 Deviation Score Ⅱ
ユーザー qwewe
提出日時 2025-05-14 13:16:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 60 ms / 2,000 ms
コード長 3,340 bytes
コンパイル時間 312 ms
コンパイル使用メモリ 82,244 KB
実行使用メモリ 78,592 KB
最終ジャッジ日時 2025-05-14 13:17:46
合計ジャッジ時間 2,600 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

# -*- coding: utf-8 -*-
import math
import sys

def solve():
    """
    Reads input, calculates the deviation score (T-score) for student M,
    and prints the truncated integer result.
    """
    # Read N (number of students)
    n = int(sys.stdin.readline())
    
    # Read the list of scores
    scores = list(map(int, sys.stdin.readline().split()))
    
    # Read M (1-based index of the student)
    m = int(sys.stdin.readline())

    # --- Deviation Score Calculation ---

    # Handle edge case: If there's only one student (N=1),
    # the standard deviation is 0. By the problem rule, the deviation score is 50.
    if n == 1:
        print(50)
        return

    # Step 1: Calculate the average score (mean)
    # Ensure floating-point division
    mean = sum(scores) / n

    # Step 2: Calculate the sum of squared differences from the mean
    sum_sq_diff = 0.0  # Use float for precision
    for score in scores:
        diff = score - mean
        sum_sq_diff += diff * diff # equivalent to diff**2

    # Step 3: Calculate the variance (average of squared differences)
    # Variance = sum of squared differences / number of students
    variance = sum_sq_diff / n
    
    # Handle potential tiny negative variance due to floating point inaccuracies
    # when the actual variance should be zero (e.g., all scores are identical).
    # Set variance to 0 if it's negligibly negative.
    if variance < 0:
        # Use a small tolerance check
         if variance > -1e-12: 
              variance = 0.0
         # Note: A significantly negative variance would indicate a problem,
         # but we assume valid inputs and standard float precision are sufficient here.

    # Step 4: Calculate the standard deviation (square root of variance)
    # math.sqrt requires a non-negative argument.
    std_dev = math.sqrt(variance)

    # Define a small tolerance (epsilon) to check if standard deviation is effectively zero.
    epsilon = 1e-9 

    # Step 5: Check the special case where standard deviation is zero.
    # As per the problem statement addendum: "if (2) [std dev] is 0, the deviation score is 50".
    if std_dev < epsilon: # Since std_dev >= 0, check if it's smaller than epsilon
        print(50)
    else:
        # Step 6: Get the score of the target student M (adjusting for 0-based index)
        score_m = scores[m - 1]

        # Step 7: Calculate the deviation score (T-score) using the standard formula:
        # T = 50 + 10 * Z-score
        # T = 50 + 10 * (score_m - mean) / std_dev
        # T = 50 + (score_m - mean) * 10 / std_dev
        # Although the problem description's step (4) is slightly ambiguous, 
        # this standard formula matches the sample case and the definition of T-scores.
        deviation_score_float = 50.0 + (score_m - mean) * 10.0 / std_dev

        # Step 8: Truncate the result towards zero.
        # The problem specifies "小数点以下切り捨て" (truncate decimal part) and clarifies
        # this means "0 方向への整数丸め" (integer rounding towards 0).
        # Python's int() function performs truncation towards zero.
        deviation_score_int = int(deviation_score_float)

        # Print the final integer result. Must be exactly an integer, no ".0".
        print(deviation_score_int)

# Execute the main calculation function
solve()
0