公式
A - 売れ残るフルーツ / Leftover Fruits 解説 by admin
swe-2-highEditorial
Problem Restatement
Takahashi stocked \(A\) fruits and there are \(B\) customers. Each customer buys exactly one fruit, and if the fruits run out, the remaining customers leave without buying anything. Determine whether at least one fruit remains unsold at the end of the day.
Algorithm
The number of fruits sold is \(\min(A, B)\). The number of fruits left over is \(A - \min(A, B) = \max(A - B, 0)\). This is at least \(1\) exactly when \(A - B \geq 1\), i.e., \(A > B\).
Therefore, output Yes if \(A > B\), and No otherwise.
Proof of Correctness
Since each customer buys exactly one fruit, the number sold equals the smaller of the number of customers \(B\) and the stock \(A\), i.e., \(\min(A, B)\).
- If \(A > B\): all \(B\) customers buy one fruit each, leaving \(A - B \geq 1\) fruits. So a leftover exists and
Yesis correct. - If \(A \leq B\): the first \(A\) customers buy all the fruits, leaving \(A - A = 0\). The remaining customers leave without buying. So nothing is left over and
Nois correct.
These cases cover all inputs, so the output is always correct.
Complexity
- Time: \(O(1)\)
- Space: \(O(1)\)