A - フィギュアスケートの採点 / Figure Skating Scoring 解説 by admin
gemini-3-flash-thinkingOverview
\(N\) athletes receive scores from \(M\) judges. The task is to calculate the “sum of \(M-2\) scores excluding the highest and lowest scores” for each athlete, and find the athlete number with the highest value (if there is a tie, the athlete with the smallest number).
Approach
To calculate the final score for each athlete, we consider the following steps: 1. Compute the sum of all \(M\) scores. 2. Find the highest and lowest scores among the \(M\) scores. 3. Subtract the highest and lowest scores from the total sum (one each).
For example, if an athlete’s scores are \([10, 20, 30, 40, 50]\), the sum is \(150\), the highest score is \(50\), and the lowest score is \(10\). The final score is \(150 - 50 - 10 = 90\).
Points to note:
- Handling ties: When multiple athletes have the same final score, we need to output the athlete with the smallest number. By processing athletes in order from \(1\) and only updating the record when the current maximum score is “strictly exceeded,” the athlete with the smaller number is naturally prioritized.
- Constraints and efficiency: Since \(N \times M\) can be as large as \(10^6\), which is relatively large, input processing needs to be efficient. In Python, techniques such as reading all input at once rather than calling input() repeatedly are effective.
Algorithm
- Initialize the current maximum score
max_final_scoreto \(-1\) and the corresponding athlete numberbest_athlete_idxto \(-1\). - For each athlete \(i = 1, 2, \dots, N\), repeat the following:
- Read the \(M\) scores of athlete \(i\).
- Compute the sum \(S\), maximum value \(Max\), and minimum value \(Min\).
- Calculate the final score \(Score = S - Max - Min\).
- If \(Score > max\_final\_score\):
- Update
max_final_scoreto \(Score\). - Update
best_athlete_idxto \(i\).
- Update
- Output the final value of
best_athlete_idx.
Complexity
- Time complexity: \(O(NM)\)
- Since we scan all scores of all athletes (a total of \(N \times M\) values) once each, the computation finishes in time proportional to the input size.
- Space complexity: \(O(NM)\)
- When using
sys.stdin.read().split()to store the input in a list all at once, all input data is held in memory, resulting in \(O(NM)\).
- When using
Implementation Notes
Efficient input: By combining
sys.stdin.read().split()withiter(), we can read a large number of values quickly and retrieve them one at a time.Using islice: By using
itertools.islice, we can efficiently extract a specific number of elements (in this case \(M\) elements) from an iterator and convert them to a list.Update condition: By using
>as the comparison operator inif final_score > max_final_score:, no update occurs when the scores are equal, so the athlete who appeared first (= with the smaller number) is retained.Source Code
import sys
from itertools import islice
def main():
# 入力をすべて読み込み、空白区切りでトークンに分割する
input_data = sys.stdin.read().split()
if not input_data:
return
# イテレータを使用してトークンを順次処理する
it = iter(input_data)
try:
n = int(next(it))
m = int(next(it))
except StopIteration:
return
max_final_score = -1
best_athlete_idx = -1
# 各選手についてスコアを計算する
for i in range(1, n + 1):
# 審査員 m 人分のスコアを整数リストとして取得する
# islice を使うことで、イテレータから必要な分だけ効率的に取り出す
scores = list(map(int, islice(it, m)))
if not scores:
break
# 最終スコアの計算: 合計から最高点と最低点を1つずつ引く
current_sum = sum(scores)
current_max = max(scores)
current_min = min(scores)
final_score = current_sum - current_max - current_min
# 最高スコアを更新した場合、選手の番号を記録する
# 同じスコアの場合は、番号が小さい方を優先するため、より大きい場合のみ更新する
if final_score > max_final_score:
max_final_score = final_score
best_athlete_idx = i
# 結果を出力する
if best_athlete_idx != -1:
print(best_athlete_idx)
if __name__ == '__main__':
main()
This editorial was generated by gemini-3-flash-thinking.