公式

A - 穴あきカード / Punched Card 解説 by admin

gemini-3-flash-preview

Overview

Given a grid (card) of size \(N \times M\), this problem asks you to punch holes (.) at \(K\) specified positions and output all other positions as not punched (#).

Key Observations

There are three key points to solving this problem:

  1. Grid Initialization: Since no holes are punched initially, we first create an \(N \times M\) grid with all cells filled with #.
  2. Coordinate Conversion: The problem gives positions using 1-based numbering (1-indexed), but arrays and lists in programs are typically managed with 0-based numbering (0-indexed). Therefore, we need to convert each input \((R_i, C_i)\) to \((R_i - 1, C_i - 1)\).
  3. Efficient I/O: Since \(N, M\) can be up to \(1000\), meaning the total number of cells can be up to \(10^6\), when handling large amounts of data in Python, it is advisable to use faster methods like sys.stdin.read and sys.stdout.write instead of the standard input() and print().

Algorithm

The implementation follows these steps:

  1. Create an \(N \times M\) 2D list (grid) and initialize all elements to '#'.
  2. Loop \(K\) times, and at each step, change grid[R_i - 1][C_i - 1] corresponding to the given coordinates \((R_i, C_i)\) to '.'.
  3. Join each row of the completed grid into a string and output them in order.

Complexity

  • Time Complexity: \(O(NM + K)\)
    • Grid initialization takes \(O(NM)\), punching holes takes \(O(K)\), and output takes \(O(NM)\). Under the given constraints, this amounts to at most about \(10^6\) operations, which is well within the time limit.
  • Space Complexity: \(O(NM)\)
    • This is the memory required to store the \(N \times M\) grid.

Implementation Notes

  • List Comprehension: Using [['#'] * M for _ in range(N)] allows efficient generation of a 2D list.

  • Fast I/O:

    • Using sys.stdin.read().split() retrieves all input at once and treats it as a list of tokens split by whitespace and newlines.

    • Using "".join(row) to combine a list into a single-line string before outputting is significantly faster than outputting one character at a time.

      Source Code

import sys

def main():
    # 入力をすべて読み込み、空白で分割してイテレータに変換します
    # 大量の入力データを高速に処理するために sys.stdin.read().split() を使用します
    data = sys.stdin.read().split()
    if not data:
        return
    
    it = iter(data)
    
    # N: 行数, M: 列数, K: 穴の数
    try:
        N = int(next(it))
        M = int(next(it))
        K = int(next(it))
    except StopIteration:
        return
    
    # N x M のグリッドを '#'(穴なし)で初期化します
    # Pythonのリスト内包表記を用いて効率的に作成します
    grid = [['#'] * M for _ in range(N)]
    
    # K 個の穴の位置 (Ri, Ci) を読み込み、グリッドを '.'(穴あり)に更新します
    # 座標は 1-indexed なので、0-indexed に変換するために 1 を引きます
    for _ in range(K):
        try:
            r = int(next(it))
            c = int(next(it))
            grid[r - 1][c - 1] = '.'
        except StopIteration:
            break
            
    # グリッドの各行を文字列に結合して出力します
    # 出力の高速化のため sys.stdout.write を使用します
    for row in grid:
        sys.stdout.write("".join(row) + "\n")

if __name__ == '__main__':
    main()

This editorial was generated by gemini-3-flash-preview.

投稿日時:
最終更新: