Official

A - 図書館の貸出管理 / Library Loan Management Editorial by admin Editorial for printing

Qwen3-Coder-480B

Overview

Given multiple types of books in a library, update the inventory according to loan requests and determine the final inventory count.

Analysis

In this problem, each loan request is given in order, and a book is lent out (reducing the inventory) only if the current inventory of the specified book is greater than or equal to the requested number of copies.

A straightforward approach of directly checking and updating the inventory for each request works perfectly fine. Looking at the constraints, the number of book types \(N\) and the number of loan requests \(M\) are at most \(10^5\), and checking and updating the inventory for each request can be done in constant time, so the overall computation is well within the time limit.

Conversely, if each request required complex processing or repeated references to the same book, we would need to consider more efficient data structures (such as prefix sums or segment trees), but that is unnecessary in this case.

The key point to watch out for in this problem is that if the requested number of copies exceeds the current inventory, the loan is not processed and the inventory remains unchanged. In other words, it is important that we cannot simply subtract and allow the result to go negative.

Algorithm

  1. First, store the initial inventory count of each book in a list books.
  2. Read each loan request in order and repeat the following:
    • If the inventory corresponding to management number \(b\) is greater than or equal to the requested number of copies \(c\), subtract \(c\) from the inventory.
  3. After all requests have been processed, output the final inventory counts in order.

Note that management numbers are 1-indexed (starting from 1), so when accessing array indices (which are 0-indexed, starting from 0), a conversion is needed (i.e., management number \(b\) corresponds to array index \(b - 1\)).

Complexity

  • Time complexity: \(O(N + M)\)
    (\(O(N)\) for reading the initial inventory, \(O(1) \times M\) for processing each request)
  • Space complexity: \(O(N)\)
    (list to store inventory information)

Implementation Notes

  • Since management numbers are 1-indexed, don’t forget to subtract -1 when accessing list indices.
  • Use conditional branching to ensure nothing happens (no update) when the inventory is insufficient.
  • For output, use something like print(*books) to print all values on a single line separated by spaces.
## Source Code

```python
# 図書館の貸出管理
# 入力読み込み
N, M = map(int, input().split())
A = list(map(int, input().split()))

# 蔵書数を管理するリスト(0-indexedで扱うため、そのまま使用)
books = A[:]

# 各貸出申請を順番に処理
for _ in range(M):
    b, c = map(int, input().split())
    index = b - 1  # 0-indexedに変換
    if books[index] >= c:
        books[index] -= c

# 結果を出力
print(*books)

This editorial was generated by qwen3-coder-480b.

posted:
last update: