A - 図書館の貸出管理 / Library Loan Management 解説 by admin 印刷ページ
DeepSeek V3Overview
This problem requires processing library lending records in order and outputting the remaining book count for each genre in each library, along with the total number of failed lending attempts.
Analysis
The problem simply involves processing each lending request in order, but the following points require attention: - If the book count is 0, the lending fails and the book count does not change - If the book count is 1 or more, the lending succeeds and the book count is decreased by 1 - Since the input size can be large (up to 200,000 lending requests), efficient processing is necessary
A straightforward approach (directly updating the book count for each lending request) is sufficiently efficient since each operation can be processed in constant time. In particular, updating the book count only requires array element access and subtraction, so it can be processed quickly.
Algorithm
- Read the input data and store the book counts for each library in a 2D array
- Process lending requests sequentially:
- Check the book count for the target library and genre
- If the book count is greater than 0, decrease it by 1
- If the book count is 0, increment the failure count
- Output the book counts for each library after processing
- Output the total number of failures
Complexity
- Time complexity: \(O(Q + \sum_{i=1}^{N} M_i)\)
- Input reading: \(O(\sum_{i=1}^{N} M_i)\)
- Lending processing: \(O(Q)\)
- Output: \(O(\sum_{i=1}^{N} M_i)\)
- Space complexity: \(O(\sum_{i=1}^{N} M_i)\)
- Memory for storing the book data
Implementation Notes
Since array indices are 0-based, subtract 1 from the input library number v and subtract 1 from the genre number d to compute the correct indices
Strictly check whether the book count is 0 (to avoid negative numbers)
Improve efficiency by reading and processing input data all at once
Use the
joinmethod when converting lists to strings for outputSource Code
import sys
def main():
data = sys.stdin.read().split()
if not data:
return
index = 0
N = int(data[index]); index += 1
libraries = []
total_books = 0
for i in range(N):
M_i = int(data[index]); index += 1
books = list(map(int, data[index:index+M_i]))
index += M_i
libraries.append(books)
total_books += M_i
Q = int(data[index]); index += 1
fail_count = 0
for i in range(Q):
v = int(data[index]); index += 1
d = int(data[index]); index += 1
library_index = v - 1
genre_index = d - 1
if libraries[library_index][genre_index] > 0:
libraries[library_index][genre_index] -= 1
else:
fail_count += 1
for books in libraries:
print(" ".join(map(str, books)))
print(fail_count)
if __name__ == "__main__":
main()
This editorial was generated by deepseekv3.
投稿日時:
最終更新: