Official

A - 常連さんを見つけよう / Find the Regulars Editorial by admin

DeepSeek V3

Overview

Given library usage records over \(D\) days, the problem asks to output in ascending order the users (regular users) who visited on \(K\) or more days.

Approach

The problem requires counting how many days each user visited and determining whether that count is \(K\) or more. Since the constraints are small with \(N, D \leq 100\), even a naive approach (checking for each user whether they visited each day) runs sufficiently fast. The key point is to correctly read the visitor list for each day and accurately count the number of days each user visited.

Algorithm

  1. Prepare an array attendance of size \(N+1\) and initialize each user’s visit count to 0.
  2. Read the data for \(D\) days, processing the visitor list for each day.
  3. For each visitor, increment the value at the corresponding index in attendance by 1.
  4. After processing all days, examine attendance and add users whose value is \(K\) or more to a list.
  5. If the list is empty, output -1; otherwise, output the users in ascending order (by number).

Complexity

  • Time complexity: \(O(D \times M_{\text{max}})\)
    • Since we process up to \(M_j\) visitors for each day. Because \(M_{\text{max}} \leq N \leq 100\), this requires at most \(100 \times 100 = 10000\) operations.
  • Space complexity: \(O(N)\)
    • Because the array recording each user’s visit count has size \(N+1\).

Implementation Notes

  • Note that the format differs between the first line of input and the subsequent lines.

  • When \(M_j = 0\), there are 0 visitors, so no processing is needed.

  • User numbers are 1-indexed, so use array indices from 1 to \(N\).

  • At the end, check whether the list of regular users is empty and output accordingly.

    Source Code

def main():
    import sys
    data = sys.stdin.read().splitlines()
    if not data:
        print(-1)
        return
        
    first_line = data[0].split()
    N = int(first_line[0])
    D = int(first_line[1])
    K = int(first_line[2])
    
    attendance = [0] * (N + 1)
    
    for i in range(1, D + 1):
        parts = data[i].split()
        if not parts:
            continue
            
        M_j = int(parts[0])
        if M_j == 0:
            continue
            
        visitors = list(map(int, parts[1:1 + M_j]))
        for visitor in visitors:
            if 1 <= visitor <= N:
                attendance[visitor] += 1
    
    frequent_visitors = []
    for i in range(1, N + 1):
        if attendance[i] >= K:
            frequent_visitors.append(i)
    
    if frequent_visitors:
        print(" ".join(map(str, frequent_visitors)))
    else:
        print(-1)

if __name__ == "__main__":
    main()

This editorial was generated by deepseekv3.

posted:
last update: