公式

B - 工場の品質検査 / Factory Quality Inspection 解説 by admin

Qwen3-Coder-480B

Overview

This is a problem of minimizing the total cost of cooling processing and losses due to defective products, based on the internal temperature of each product.

Analysis

In this problem, if the internal temperature \(W_i\) of each product is at least the threshold \(T\), it is considered “defective,” and shipping it incurs a loss of \(D\). However, the loss can be avoided by performing cooling processing. Cooling costs \(C\).

For each defective product, we can choose either “cool it (cost \(C\))” or “accept the loss without cooling (loss \(D\)).” A naive approach of trying all patterns of “cool or not cool” would result in an explosion of computation (e.g., \(2^N\) possibilities). However, since the choice for each product is independent, the optimal strategy is to simply choose the smaller of cost \(C\) and loss \(D\) for each defective product.

In other words, the answer is the sum of \(\min(C, D)\) over all defective products.

Also, products with internal temperature below \(T\) are not defective, so neither cooling nor loss is incurred for them, and they can be ignored.

For example, consider the following input:

N=3, T=5, C=2, D=3
W = [4, 6, 7]
  • Product 1: Temperature 4 → Not defective → Do nothing
  • Product 2: Temperature 6 → Defective → Cooling(2) or Loss(3) → Cooling is cheaper
  • Product 3: Temperature 7 → Defective → Cooling(2) or Loss(3) → Cooling is cheaper

Total cost = 2 + 2 = 4

Algorithm

  1. Extract only the products whose internal temperature is at least \(T\) (defective product candidates).
  2. For each extracted product, choose the smaller of “cooling cost \(C\)” and “loss \(D\).”
  3. Output the sum of these values.

Complexity

  • Time complexity: \(O(N)\)
  • Space complexity: \(O(N)\)

Implementation Notes

  • sys.stdin.read is used for fast input reading.
  • Since non-defective products can be ignored, filtering them out first allows for efficient processing.
  • Since each defective product can be judged independently, it suffices to simply accumulate min(C, D) within a loop.
## Source Code

```python
import sys
input = sys.stdin.read

def main():
    data = input().split()
    N = int(data[0])
    T = int(data[1])
    C = int(data[2])
    D = int(data[3])
    W = list(map(int, data[4:]))

    # 不良品となる製品のみを抽出(W_i >= T)
    defective = [w for w in W if w >= T]
    
    # 不良品がなければコストは0
    if not defective:
        print(0)
        return
    
    # コスト比較:冷却処理(C) vs 損失(D)
    # 各不良品について、冷却するかしないかの選択がある
    # 冷却コスト C と 損失 D のどちらがお得か比較
    total_cost = 0
    for _ in defective:
        total_cost += min(C, D)
    
    print(total_cost)

if __name__ == "__main__":
    main()

This editorial was generated by qwen3-coder-480b.

投稿日時:
最終更新: