A - 荷物の仕分け / Sorting Packages Editorial by admin Editorial for printing
Qwen3-Coder-480BOverview
This is a problem where you classify packages into “this month” and “next month” based on their scheduled delivery date (year and month), and compute the total weight for each.
Analysis
In this problem, for each package, we need to determine whether its scheduled delivery date \(P_i\) year \(Q_i\) month matches “this month” (\(Y\) year \(M\) month) or “next month”. If it matches, we add its weight \(C_i\) to the corresponding total.
The key point here is how to calculate “next month.” For ordinary months, it is simply month \(M+1\), but if the current month is December, it rolls over to the next year, so we need to increment the year by 1 and reset the month to 1.
For example, if this month is December \(2025\), then next month is January \(2026\).
Since the year-month combination changes depending on this condition, we cannot correctly determine the result by simple string comparison or numerical magnitude comparison alone. Therefore, we must first accurately compute the year-month pair representing “this month” and “next month,” and then compare each package’s scheduled delivery date against them.
Also, since the constraints are very small (\(N \leq 100\)), a brute-force approach is more than sufficient. No more efficient algorithm is needed.
Algorithm
- First, compute the year-month pair for “next month” \((next\_Y, next\_M)\) from the year-month of “this month” \((Y, M)\).
- For each package, check if the scheduled delivery date \((P_i, Q_i)\):
- Matches “this month” → add weight \(C_i\) to
current_total - Matches “next month” → add weight \(C_i\) to
next_total
- Matches “this month” → add weight \(C_i\) to
- Finally, output
current_totalandnext_total.
Here, the comparison for “this month” and “next month” simply checks whether the year-month pairs are equal, and no special optimization is needed.
Concrete Example
Sample input:
3 2025 12
2025 12 2025 12 100
2025 12 2026 1 200
2025 11 2025 11 300
- This month: December \(2025\)
- Next month: January \(2026\)
- Package 1: Scheduled delivery is this month → add 100g to this month’s total
- Package 2: Scheduled delivery is next month → add 200g to next month’s total
- Package 3: Scheduled delivery is another month → ignored
Result: 100 200
Complexity
- Time complexity: \(O(N)\)
- Space complexity: \(O(1)\)
Since each package is processed in constant time, all \(N\) packages are processed in linear time overall. Additionally, since only a fixed number of variables are used, the space is constant.
Implementation Notes
When computing “next month,” be careful that if the month is 12, you must increment the year by 1 and set the month to 1
Compare each package’s scheduled delivery date against “this month” and “next month” using year-month pairs
Packages that match neither should be ignored (not added to any total)
Source Code
def main():
import sys
input = sys.stdin.read
data = input().split()
idx = 0
N = int(data[idx]); idx += 1
Y = int(data[idx]); idx += 1
M = int(data[idx]); idx += 1
# 今月 (Y, M)
# 翌月を計算
if M == 12:
next_Y = Y + 1
next_M = 1
else:
next_Y = Y
next_M = M + 1
current_total = 0
next_total = 0
for _ in range(N):
A = int(data[idx]); idx += 1
B = int(data[idx]); idx += 1
P = int(data[idx]); idx += 1
Q = int(data[idx]); idx += 1
C = int(data[idx]); idx += 1
if P == Y and Q == M:
current_total += C
elif P == next_Y and Q == next_M:
next_total += C
print(current_total, next_total)
if __name__ == "__main__":
main()
This editorial was generated by qwen3-coder-480b.
posted:
last update: