A - 果物の選別 / Fruit Sorting Editorial by admin
gemini-3-flash-thinkingOverview
This is a problem where, among \(N\) fruits, you need to find the average sugar content of those with sugar content at least \(K\). If no fruits satisfy the condition, you need to output \(-1\) as a special value.
Analysis
To compute the average, you need to divide the “total sugar content of fruits satisfying the condition” by the “number of fruits satisfying the condition.”
There are three key points to be careful about when solving this problem:
- Filtering the data:
Check each of the given \(N\) sugar content values \(S_1, S_2, \dots, S_N\) in order, add only those satisfying \(S_i \geq K\) to the total (
total_sum), and count how many there are (count). - Handling large input:
Since \(N\) can be as large as \(10^6\), repeatedly calling Python’s standard
input()function may exceed the time limit (TLE). It is effective to usesys.stdin.read().split()to read all input at once for speedup. - Avoiding division by zero:
If \(0\) fruits satisfy the condition, attempting to compute the average results in a “division by zero” error. As specified in the problem statement, when the count is \(0\), you must output
-1without performing the calculation.
Algorithm
- Read the number of fruits \(N\), the threshold \(K\), and the \(N\) sugar content values \(S\).
- Initialize
total_sumandcountto \(0\). - For each sugar content \(S_i\), repeat the following:
- If \(S_i \geq K\), add \(S_i\) to
total_sumand incrementcountby \(1\).
- If \(S_i \geq K\), add \(S_i\) to
- After the loop ends:
- If
countis \(0\), output-1. - If
countis \(1\) or more, compute and outputtotal_sum / count.
- If
Complexity
- Time complexity: \(O(N)\) Since we scan the \(N\) data points once each, the computation finishes in time proportional to the input size. Even for \(N=10^6\), this is well within the time limit.
- Space complexity: \(O(N)\) When reading all input at once and storing it in a list, \(O(N)\) space is needed to hold the \(N\) elements in memory.
Implementation Notes
Fast I/O: In Python, using
sys.stdin.read().split()allows you to quickly obtain a large number of values as a list.Precision: Python’s floating-point numbers (
floattype) internally have precision equivalent to C’sdouble, which is more than sufficient to meet this problem’s required precision (relative error of \(10^{-6}\) or less).Large values: The total
total_sumcan be as large as approximately \(10^6 \times 10^9 = 10^{15}\), but since Python automatically extends the number of digits for integers (inttype), there is no need to worry about overflow.Source Code
import sys
def solve():
# 全ての入力を一度に読み込み、空白(スペースや改行)で分割してトークンのリストを作成します。
# N=10^6 程度の入力であれば、この方法がPythonにおいて高速です。
input_data = sys.stdin.read().split()
# 入力が空の場合や不足している場合は終了します。
if len(input_data) < 2:
return
# 最初の2つのトークンは果物の個数 N と出荷基準の糖度 K です。
n = int(input_data[0])
k = int(input_data[1])
# 出荷基準を満たす果物の糖度の合計と個数を管理します。
total_sum = 0
count = 0
# 糖度のリストは input_data[2] から input_data[n+1] までです。
# map(int, ...) を使用して一括で整数に変換しながらループを回します。
# スライス input_data[2:2+n] を用いることで正確に N 個のデータを取得します。
for s in map(int, input_data[2:2+n]):
if s >= k:
total_sum += s
count += 1
# 出荷できる果物が1つもない場合は -1 を出力します。
if count == 0:
print("-1")
else:
# 平均値を計算して出力します。
# Pythonの浮動小数点数(double精度)は十分な精度(約15〜17桁)を持っており、
# 相対誤差 10^-6 以下の要求を満たすことができます。
print(total_sum / count)
if __name__ == "__main__":
solve()
This editorial was generated by gemini-3-flash-thinking.
posted:
last update: