公式

A - 荷物の仕分け / Sorting Packages 解説 by admin

GPT 5.2 High

Overview

For each package, determine whether its “scheduled delivery date (year and month)” matches the current month \(Y\)/\(M\) or the following month, then sum up the weights for each category and output them.

Analysis

  • The sorting uses only the scheduled delivery date \((P_i, Q_i)\). Although the acceptance date \((A_i, B_i)\) appears in the conditions, it is not used at all. Mixing these up can cause WA.
  • We only need the following two types of packages:
    1. \((P_i, Q_i) = (Y, M)\) (current month)
    2. \((P_i, Q_i) = (\text{next month's year}, \text{next month's month})\) (next month)
  • The key edge case when calculating “next month” is when \(M=12\). For example, the month after December \(2025\) is January \(2026\).
    In all other cases, simply add \(+1\) to the month.
  • Since \(N \le 100\), a straightforward approach of iterating through each package once and accumulating the totals is sufficiently fast. Over-complicating things by converting dates to numerical values and computing differences tends to introduce bugs at boundaries (December → January).

Algorithm

  1. Read the current month \((Y, M)\) from input.
  2. Use a function next_month(Y, M) to compute the next month \((ny, nm)\).
    • If \(M=12\), then \((Y+1, 1)\)
    • Otherwise, \((Y, M+1)\)
  3. Initialize sum_cur (total for current month) and sum_next (total for next month) to \(0\).
  4. For each package, examine the scheduled delivery date \((P, Q)\):
    • If \((P, Q) = (Y, M)\), then sum_cur += C
    • If \((P, Q) = (ny, nm)\), then sum_next += C
    • Otherwise, ignore it
  5. Output sum_cur sum_next.

(Example) If the current month is \((2025, 12)\), the next month is \((2026, 1)\), so packages with a scheduled delivery of \((2026, 1)\) are added to the next month’s total.

Complexity

  • Time complexity: \(O(N)\) (just checking and adding each package once)
  • Space complexity: \(O(1)\) (only a constant number of variables such as the totals)

Implementation Notes

  • For the next month calculation, only December needs special handling (the year increments).

  • Comparisons should be done by comparing year and month as a pair, e.g., (P == Y and Q == M).

  • The acceptance date \((A_i, B_i)\) needs to be read from input, but it is not used in the logic (in the code, it is discarded as _A, _B).

    Source Code

import sys

def next_month(y, m):
    if m == 12:
        return y + 1, 1
    return y, m + 1

def main():
    data = sys.stdin.read().strip().split()
    if not data:
        return
    it = iter(map(int, data))
    N = next(it)
    Y = next(it)
    M = next(it)

    ny, nm = next_month(Y, M)
    sum_cur = 0
    sum_next = 0

    for _ in range(N):
        _A = next(it)
        _B = next(it)
        P = next(it)
        Q = next(it)
        C = next(it)
        if P == Y and Q == M:
            sum_cur += C
        elif P == ny and Q == nm:
            sum_next += C

    print(f"{sum_cur} {sum_next}")

if __name__ == "__main__":
    main()

This editorial was generated by gpt-5.2-high.