Official

B - 果物の選別 / Fruit Sorting Editorial by admin

Gemini 3.0 Flash

Overview

Given \(N\) harvested fruits, this problem asks you to find the number of “substandard” fruits that satisfy both conditions — “has a blemish” AND “sweetness level is less than \(K\)” — as well as the total sweetness of those fruits.

Analysis

The key to solving this problem is to organize “which fruits should be checked.”

A fruit is substandard if it simultaneously satisfies these two conditions: 1. The fruit has a blemish (its number is one of \(B_1, \dots, B_M\)) 2. The fruit’s sweetness \(A_i\) is less than \(K\)

If we check all \(N\) fruits one by one, searching each time whether it is “included in the blemish list \(B\),” the list search takes too much time (it becomes \(O(N \times M)\), which won’t fit within the time limit).

However, in this problem, the numbers of the blemished fruits are given upfront as \(M\) values. Therefore, by only considering the blemished fruits from the start and checking whether their sweetness is less than \(K\), we can efficiently find the answer.

Algorithm

  1. Store sweetness data: Store the sweetness values \(A_1, A_2, \dots, A_N\) of all fruits in an array (list). The sweetness of fruit number \(i\) can be quickly accessed as the \((i-1)\)-th element of the array.
  2. Iterate over blemished fruits: Take each of the \(M\) given “blemished fruit numbers \(B_j\)” one by one.
  3. Condition check:
    • Retrieve the sweetness \(A_{B_j}\) of fruit \(B_j\) from the array.
    • If \(A_{B_j} < K\), then the fruit is substandard.
    • Add 1 to the “count of substandard fruits” and add \(A_{B_j}\) to the “total sweetness.”
  4. Output: Print the final count and total.

Complexity

  • Time complexity: \(O(N + M)\)
    • Reading input takes \(O(N + M)\), and checking the \(M\) blemished fruits takes \(O(M)\). Since \(N, M \leq 2 \times 10^5\), this runs sufficiently fast.
  • Space complexity: \(O(N)\)
    • An array of length \(N\) is used to store the sweetness of all fruits.

Implementation Notes

  • 1-indexed vs 0-indexed: Fruit numbers in the problem start from \(1\), but arrays in programming languages typically start from \(0\). When accessing the sweetness of fruit \(B_j\), you need to adjust the index to B[j] - 1.

  • Fast I/O: Since \(N\) and \(M\) can be large, in Python you can reduce execution time by reading all input at once using sys.stdin.read().split(), etc.

  • Handling large numbers: The total sweetness can become a very large value, but since Python natively supports arbitrary-precision integers, you can compute without worrying about overflow.

    Source Code

import sys

def main():
    # 標準入力からすべてのデータを読み込み、空白文字で分割してリストにする
    # この手法は大量の入力を高速に処理するのに適している
    try:
        data = sys.stdin.read().split()
    except EOFError:
        return
    
    if not data:
        return
    
    # N: 果物の総数
    # M: 傷のある果物の個数
    # K: 高級品として扱う基準となる糖度
    n = int(data[0])
    m = int(data[1])
    k = int(data[2])
    
    # A: 各果物の糖度 (1番目からN番目まで)
    # インデックスによる高速なアクセスのため、整数のリストに変換して保持する
    # a[0] が果物1の糖度 A_1 に対応する
    a = list(map(int, data[3:3+n]))
    
    # B: 傷のある果物の番号 (M個)
    # 傷のある果物のインデックスは data[3+n] 以降に格納されている
    b_indices = map(int, data[3+n:3+n+m])
    
    # 規格外品として出荷すべき果物の個数と糖度の合計を管理する変数
    count = 0
    total_sugar = 0
    
    # 傷のある果物それぞれについて、糖度が K 未満であるか判定する
    # 規格外品の条件:傷があり、かつ糖度が K 未満
    for fruit_idx in b_indices:
        # 果物の番号は1から始まるため、リストのインデックス(0から開始)に合わせる
        sugar_content = a[fruit_idx - 1]
        
        if sugar_content < k:
            count += 1
            total_sugar += sugar_content
    
    # 結果を1行にスペース区切りで出力する
    print(count, total_sugar)

if __name__ == "__main__":
    main()

This editorial was generated by gemini-3-flash-preview.

posted:
last update: