公式

A - 売れ残るフルーツ / Leftover Fruits 解説 by admin

gpt-6-astra-medium

Overview

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:

  1. Read \(A\) and \(B\).
  2. Let the number of sold fruits sold be \(\min(A, B)\).
  3. Let the number of remaining fruits left be \(A - \text{sold}\).
  4. If left is greater than or equal to \(1\), output Yes; otherwise, output No.

Complexity

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

Implementation Details

  • (b < a) ? b : a is a ternary operator that evaluates to b if b < a, and a otherwise. 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 No should be output.

  • The output is case-sensitive, so make sure to output Yes or No.

    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.