公式

B - 教室の割り当て / Classroom Assignment 解説 by admin

Qwen3-Coder-480B

Overview

Each day, multiple visitors have preferred classrooms, and if the number of visitors exceeds the capacity, the lecture in that classroom is canceled. We need to find the total number of people who were able to attend lectures.

Analysis

In this problem, for each day, we tally the number of visitors who want each classroom and compare it with that classroom’s capacity to determine the number of participants for that classroom on that day.

For example, if on a certain day classroom A has a capacity of 10 and there are 8 applicants, then everyone can attend. However, if there are 12 applicants, the lecture is canceled for safety reasons, and no one can attend.

A naive approach would be to process the list of visitors directly each time and count the number of people per classroom, but in the worst case, this can be inefficient when \(K_i\) is large for a given day. However, since all we need for each day is “how many people want each classroom,” we can process this efficiently by counting in advance.

Also, since we need to read input quickly, it is effective to use sys.stdin.read to read all input at once, then split and process it.

Algorithm

  1. Read all input at once and split it into tokens.
  2. For each day, do the following:
    • Get the number of visitors \(K_i\) for that day.
    • Read each visitor’s preferred classroom and convert to 0-indexed.
    • Use Counter to tally the number of applicants for each classroom.
    • For each classroom, if the number of applicants is less than or equal to the capacity, add that number to the answer. If it exceeds the capacity, no one can attend, so ignore it.
  3. Output the total across all days.

Complexity

  • Time complexity: \(O(\sum_{i=1}^{N} K_i)\)
  • Space complexity: \(O(M + \max(K_i))\)

※ Since \(\sum K_i \leq 2 \times 10^5\), this is sufficiently fast.

Implementation Notes

  • Classroom numbers are given as 1-indexed in the input, so when handling them internally, convert to 0-indexed (subtract 1).
  • By using Counter, we can efficiently tally the number of applicants for each classroom.
  • Since the input can be very large, it is necessary to use sys.stdin.read to read input quickly.
## Source Code

```python
import sys
from collections import Counter

input = sys.stdin.read

def main():
    data = input().split()
    idx = 0
    
    N = int(data[idx])
    idx += 1
    M = int(data[idx])
    idx += 1
    
    C = [int(data[idx + j]) for j in range(M)]
    idx += M
    
    total = 0
    
    for _ in range(N):
        K = int(data[idx])
        idx += 1
        P = [int(data[idx + k]) - 1 for k in range(K)]
        idx += K
        
        count = Counter(P)
        
        for room, cnt in count.items():
            if cnt <= C[room]:
                total += cnt
                
    print(total)

if __name__ == "__main__":
    main()

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

投稿日時:
最終更新: