A - 売れ残るフルーツ / Leftover Fruits 解説 by admin
gpt-6-astra-mediumOverview
This problem asks whether at least \(1\) fruit remains after \(B\) customers make their purchases from an initial stock of \(A\) fruits.
Analysis
Each customer buys \(1\) fruit, but you cannot sell more fruits than the stocked amount. Therefore, the actual number of fruits sold is \(\min(A, B)\).
Thus, the number of remaining fruits can be calculated as follows:
\(A - \min(A, B)\)
For example, if \(A = 5\) and \(B = 3\), \(3\) fruits are sold and \(2\) remain, so the answer is Yes. On the other hand, if \(A = 3\) and \(B = 5\), all \(3\) fruits are sold and the remaining customers leave without buying anything, so the answer is No.
A method that processes customers one by one and decrements the stock would also easily run within the time limit under these constraints. However, there is no need to actually simulate the process, as it can be computed directly using the formula above.
Note that when \(A = B\), all fruits are completely sold out as well. Therefore, if you write the condition using a direct comparison, it must be \(A > B\), not \(A \geq B\).
Algorithm
In the presented code, the number of sold fruits and remaining fruits are computed step-by-step:
- Read \(A\) and \(B\).
- Let the number of sold fruits
soldbe \(\min(A, B)\). - Let the number of remaining fruits
leftbe \(A - \text{sold}\). - If
leftis greater than or equal to \(1\), outputYes; otherwise, outputNo.
Complexity
- Time complexity: \(O(1)\)
- Space complexity: \(O(1)\)
Implementation Details
(b < a) ? b : ais a ternary operator that evaluates tobifb < a, andaotherwise. This is used to compute the actual number of sold fruits, \(\min(A, B)\).When \(A = B\), the number of remaining fruits is \(0\), so
Noshould be output.The output is case-sensitive, so make sure to output
YesorNo.Source Code
// 問題文から独立に実装: 売れ残り個数を計算して 1 以上か
#include <iostream>
int main() {
int a, b; std::cin >> a >> b;
int sold = (b < a) ? b : a;
int left = a - sold;
std::cout << (left >= 1 ? "Yes" : "No") << std::endl;
}
This editorial was generated by gpt-6-astra-medium.