B - 工場の品質検査 / Factory Quality Inspection 解説 by admin
DeepSeek V3Overview
For each product, if the internal temperature is at or above the threshold \(T\), we must choose either additional cooling treatment (cost \(C\)) or shipping as a defective product (loss \(D\)), and we want to minimize the total cost.
Analysis
Each product can be handled independently. Products with internal temperature below \(T\) do not become defective, so no cost is incurred without doing anything. For products with internal temperature at or above \(T\), we can either apply additional cooling treatment (cost \(C\)) or ship as-is and incur loss \(D\). Therefore, for each such product, we simply choose the smaller of \(C\) and \(D\).
Algorithm
- Check the internal temperature \(W_i\) of each product in order.
- If \(W_i < T\), no cost is incurred.
- If \(W_i \geq T\), add \(\min(C, D)\) to the total cost.
This algorithm is very efficient since we only need to look at each product once.
Complexity
- Time complexity: \(O(N)\)
- Space complexity: \(O(1)\) (excluding input data reading)
Implementation Notes
After reading the input, process each product’s temperature in a loop.
The conditional logic is simple: add \(\min(C, D)\) only when \(W_i \geq T\).
If the internal temperature is below \(T\), simply skip without doing anything.
Source Code
def main():
import sys
data = sys.stdin.read().split()
n = int(data[0])
T = int(data[1])
C = int(data[2])
D = int(data[3])
W_list = list(map(int, data[4:4+n]))
cost_if_cool = C
cost_if_not = D
total_cost = 0
for w in W_list:
if w >= T:
if cost_if_cool < cost_if_not:
total_cost += cost_if_cool
else:
total_cost += cost_if_not
else:
pass
print(total_cost)
if __name__ == "__main__":
main()
This editorial was generated by deepseekv3.
投稿日時:
最終更新: