A - 荷物の仕分け / Sorting Packages 解説 by admin
Claude 4.6 Opus (Thinking)Overview
This is a problem where you check the scheduled delivery year and month for each of \(N\) packages, classify them into the current month’s and next month’s deliveries, and find the total weight for each.
Analysis
The important points in this problem are as follows.
1. The reception year and month are not used
The problem statement explicitly states “the reception year and month are not used for classification at all.” Although the input includes the reception year and month (\(A_i\), \(B_i\)), only the scheduled delivery year and month (\(P_i\), \(Q_i\)) are used for the determination. You need to read them in without skipping, but simply ignore them.
2. Be careful when calculating the next month (December → January of the next year)
For example, if the current month is December \(2025\), the next month is not month \(13\) of \(2025\) but January \(2026\). You need to handle this case correctly.
- When \(M = 12\): the next month is January of year \((Y+1)\)
- When \(M \neq 12\): the next month is month \((M+1)\) of year \(Y\)
3. A straightforward approach is sufficient
Since \(N \leq 100\) is small, a simple loop that checks and classifies each package one by one is sufficient. No computational optimizations are needed.
Algorithm
- From the current year and month \((Y, M)\), calculate the next month’s year and month \((NY, NM)\).
- Initialize the total weight for the current month
this_monthand the total weight for the next monthnext_monthto \(0\). - For each of the \(N\) packages:
- If the scheduled delivery year and month \((P_i, Q_i)\) matches \((Y, M)\), add the weight \(C_i\) to
this_month. - If the scheduled delivery year and month \((P_i, Q_i)\) matches \((NY, NM)\), add the weight \(C_i\) to
next_month. - If it matches neither, ignore it.
- If the scheduled delivery year and month \((P_i, Q_i)\) matches \((Y, M)\), add the weight \(C_i\) to
- Output
this_monthandnext_month.
Concrete example: When \(Y=2025\), \(M=12\), \(N=3\), the next month is \((2026, 1)\).
| Package | Scheduled delivery | Weight | Classification |
|---|---|---|---|
| 1 | December 2025 | 500 | Current month |
| 2 | January 2026 | 300 | Next month |
| 3 | November 2025 | 200 | Ignored |
Output: 500 300
Complexity
- Time complexity: \(O(N)\) — just checking each package once
- Space complexity: \(O(1)\) — only variables to hold the total weights are used
Implementation Tips
Calculate the next month beforehand: Instead of calculating it every iteration inside the loop, computing \((NY, NM)\) in advance makes the code cleaner.
Using
elif: Due to the problem’s constraints, a package cannot belong to both the current month and the next month (since the current month and next month are different year-months). Therefore, usingif/eliffor mutually exclusive checks is perfectly fine.When there are no matching packages: If you initialize the values to \(0\), then even when there are no matching packages, \(0\) will be output as-is, so no special handling is needed.
Source Code
N, Y, M = map(int, input().split())
if M == 12:
NY, NM = Y + 1, 1
else:
NY, NM = Y, M + 1
this_month = 0
next_month = 0
for _ in range(N):
A, B, P, Q, C = map(int, input().split())
if P == Y and Q == M:
this_month += C
elif P == NY and Q == NM:
next_month += C
print(this_month, next_month)
This editorial was generated by claude4.6opus-thinking.