USACO 2026 First Contest, Bronze Problem 1. Chip Exchange

https://usaco.org/index.php?page=viewproblem2&cpid=1539

英文注释完整代码 + 贪心思路完整英文解析

1. Greedy Thought Explanation (贪心思路英文说明)

Core Idea

We use a greedy strategy: convert all convertible B chips to A chips first. If the current amount of A already meets the target fa, answer is 0. Otherwise split into two cases based on the size of cb and ca:

  1. Case 1: cb < ca
    The worst scenario is giving lack_A - 1 A chips (just one short of target A), then we need extra cb - B B chips to perform one more exchange to hit the target. The total extra chips needed here is lack_A - 1 + cb - B (expr1 in code).

  2. Case 2: cb >= ca
    We need to calculate the total B chips required to exchange enough A to cover the shortage lack_A.

  • Compute total exchange rounds with ceiling division: required_swap = ceil(lack_A / ca)
  • Total B consumed for all exchanges: total_B_need = required_swap * cb
  • We have leftover B B chips, so base deficit of B is total_B_need - B
  • We add (lack_A - 1) % ca extra A chips to cover the incomplete final exchange margin.
    This combined value is expr2 = (lack_A - 1) % ca + total_B_need - B.

Final Answer Rule

The minimal guaranteed extra chips x must satisfy both worst-case scenarios above, so we take the maximum of expr1 and expr2.

2. Full Code with All English Comments

# Read the integer representing total number of independent test cases
T = int(input())

# Iterate and solve every test case one by one
for _ in range(T):
    # Input five integers for current case parameters
    # A: initial count of type A chips
    # B: initial count of type B chips
    # ca: quantity of A obtained per exchange operation
    # cb: quantity of B consumed per exchange operation
    # fa: target minimal number of A chips we need to guarantee
    A, B, ca, cb, fa = map(int, input().split())

    # Step 1: Convert all fully exchangeable B chips into A chips (greedy preprocess)
    # Calculate how many complete exchanges we can do with current B stock
    swap_times = B // cb
    # Add all A chips gained from exchanges to original A inventory
    A = A + swap_times * ca
    # Remainder B chips that cannot form a full exchange group
    B = B % cb

    # Calculate the deficit of A chips to reach target fa
    lack_A = fa - A

    # If existing A already meets or exceeds target, zero extra chips are needed
    if lack_A <= 0:
        print(0)
    else:
        # Formula 1: worst case when cb < ca
        expr1 = lack_A - 1 + cb - B

        # Formula 2: worst case when cb >= ca
        # Remainder margin to compensate incomplete exchange of A
        mod_part = (lack_A - 1) % ca
        # Ceiling division: total exchange rounds required to cover A shortage
        required_swap = (lack_A + ca - 1) // ca
        # Total B chips consumed to finish all required exchange rounds
        total_B_need = required_swap * cb
        # Combine B deficit and A margin compensation
        expr2 = mod_part + total_B_need - B

        # We must satisfy both worst-case scenarios, take the larger value
        ans = max(expr1, expr2)
        # Output minimal non-negative x for current test case
        print(ans)

3. Formula Correspondence to Your Description

  1. expr1 = lack_A - 1 + cb - B
    Matches your statement for cb < ca: give lack_A - 1 A chips, plus cb - B B chips.
  2. expr2 = (lack_A - 1) % ca + ((lack_A + ca - 1) // ca) * cb - B
    Matches your statement for cb >= ca:
  • ((lack_A + ca - 1) // ca) * cb = total B chips needed for all exchanges
  • Subtract owned leftover B B
  • Add (lack_A - 1) % ca extra A chips to cover incomplete exchange margin
  1. ans = max(expr1, expr2)
    We pick the larger value because x must work for any random mix of added chips, covering both extreme worst cases.
posted @ 2026-06-23 00:11  kkman2000  阅读(9)  评论(0)    收藏  举报