A - 図書館の貸出管理 / Library Loan Management 解説 by admin
DeepSeek V3Overview
This is a problem about simulating a library inventory management system. We process each book’s initial stock count and loan requests in order, then determine the final stock count.
Analysis
This problem is a straightforward simulation where we process the given loan requests in order. The key point is that for each request, we only execute the loan and reduce the stock count if the current stock of the book is greater than or equal to the requested number of copies. If the stock is insufficient, no changes are made.
A naive approach is to directly update the array elements for each loan request. This method is intuitive, easy to understand, and sufficiently efficient within the given constraints.
Algorithm
- Read the array
Astoring the initial stock counts - Process M loan requests in order
- For each request:
- Calculate the array index from the book’s management number
b(b-1) - Check whether the current stock count
A[b-1]is greater than or equal to the requested number of copiesc - Only if the condition is satisfied, subtract
cfrom the stock count
- Calculate the array index from the book’s management number
- After all processing, output the final stock counts
Complexity
- Time complexity: \(O(M)\)
- Because each of the M loan requests is processed in constant time
- Space complexity: \(O(N)\)
- Because the array storing the stock counts has size N
Implementation Notes
Management numbers are 1-indexed, but the array is 0-indexed, so care must be taken with index calculation (
b-1)Processing each request involves only simple comparison and subtraction, where the conditional branching is important
Reading input data is done all at once for efficiency
When outputting, the list of numbers is converted to strings and printed with space separation
Source Code
import sys
def main():
data = sys.stdin.read().split()
n = int(data[0])
m = int(data[1])
A = list(map(int, data[2:2+n]))
operations = []
index = 2 + n
for i in range(m):
b = int(data[index])
c = int(data[index+1])
index += 2
operations.append((b, c))
for b, c in operations:
idx = b - 1
if A[idx] >= c:
A[idx] -= c
print(" ".join(map(str, A)))
if __name__ == "__main__":
main()
This editorial was generated by deepseekv3.