A - 荷物の仕分け / Sorting Packages 解説 by admin
DeepSeek V3Overview
This is a problem where you determine whether the scheduled delivery month of each package falls on the current month (\(Y\)-year, \(M\)-month) or the following month, and calculate the total weight for each.
Analysis
The core of the problem is accurately determining whether each package’s scheduled delivery year and month corresponds to “this month” or “next month.” When calculating the next month, note that if the current month is December, the year rolls over. Given the constraints, the number of packages \(N\) is at most 100, which is small, so a simple brute-force approach is sufficiently efficient. The acceptance year and month are not used in the determination, so they can be ignored.
Algorithm
- First, calculate the month following the current month (\(Y\)-year, \(M\)-month). If \(M\) is 12, increment the year by 1 and set the month to 1; otherwise, simply increment the month by 1.
- For each package, check whether the scheduled delivery year and month (\(P_i\)-year, \(Q_i\)-month) matches the current month. If it matches, add the weight to the current month’s total.
- If it does not match the current month, check whether it matches the next month. If it matches, add the weight to the next month’s total.
- Packages that match neither are ignored.
- Finally, output the total weights for the current month and the next month.
Complexity
- Time complexity: \(O(N)\)
- Since each package is processed exactly once, it is proportional to \(N\).
- Space complexity: \(O(1)\)
- Only constant-sized variables are used to read the input data.
Implementation Notes
By computing the next month in advance and storing it in a variable, we avoid recalculating it for each package’s check.
By reading all input data at once, splitting it, and processing with an advancing index, the data is handled efficiently.
The conditional branching has a simple structure: first check if it matches the current month, then check if it matches the next month.
Source Code
def main():
import sys
data = sys.stdin.read().split()
n = int(data[0])
Y = int(data[1])
M = int(data[2])
index = 3
this_month_total = 0
next_month_total = 0
next_year = Y
next_month = M + 1
if next_month > 12:
next_month = 1
next_year = Y + 1
for i in range(n):
A = int(data[index])
B = int(data[index+1])
P = int(data[index+2])
Q = int(data[index+3])
C = int(data[index+4])
index += 5
if P == Y and Q == M:
this_month_total += C
elif P == next_year and Q == next_month:
next_month_total += C
print(f"{this_month_total} {next_month_total}")
if __name__ == "__main__":
main()
This editorial was generated by deepseekv3.