1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[ABC436] ABC 436(Atcoder Beginner Contest)のA~C(A,B,C)問題をPythonで解説(復習)

Posted at

[ABC436] ABC 436(Atcoder Beginner Contest)のA~C(A,B,C)問題をPythonで解説(復習)

A問題

  • len を使って足りない長さを前に入れれば良い。
A.py
"""
<方針>
- `len` を使って足りない長さを前に入れれば良い。
"""
# 入力
N = int(input())
S = input()

# 前の部分
head = "o" * (N - len(S))

# 答え
ans = head + S

# 出力
print(ans)

B問題

  • 盤面 B を用意し、順番に処理していけば良い()()()
B.py
"""
<方針>
- 盤面 `B` を用意し、順番に処理していけば良い()()()
"""
N = int(input())

# 盤面
B = [[0]*N for _ in range(N)]

# 現在地
r = 0
c = (N-1)//2

# 初期値
B[r][c] = 1

# 順番に処理
for i in range(N**2-1):
  # 現在値
  v = i + 2
  
  # 現在地
  r = (r-1)%N
  c = (c+1)%N
  
  # すでに値があったら、
  if(B[r][c] != 0):
    # 元に戻す
    r = (r+2)%N
    c = (c-1)%N
  
  # 値代入
  B[r][c] = v

# 出力
for i in range(N):
  print(*B[i])

C問題

方針

  • N がデカすぎて、盤面を保持すること O(N^2) はできない
  • そこで、集合を用いて、盤面を管理する。
  • 入れるときは4マス入る or NOT でやれば良い。

前提

  • C問題あたりで,TLEになる人は,制約条件を見る癖をつけよう.
  • AB問題では,基本的に制約条件を見ずにやっても解ける.
  • しかし,C問題以降では,制約条件を見ないと必ずTLEすると思っても良い.
  • 詳しい話は私の352回の記事C問題の解説に記したので,是非参照してほしい.
C.py
"""
<方針>
- `N` がデカすぎて、盤面を保持すること `O(N^2)` はできない
- そこで、集合を用いて、盤面を管理する。
- 入れるときは4マス入る or NOT でやれば良い。
"""
N, M = map(int, input().split())

# 盤面
se = set()

# 4マスを取得
def block(R, C):
  return [(dr+R)*(N+1) + (dc+C) for dr, dc in [
    (+0, +0),
    (+1, +0), 
    (+0, +1), 
    (+1, +1), 
    ]]

for _ in range(M):
  R, C = map(int, input().split())
  
  # 盤面にブロックがおけるか
  ok = True
  for key in block(R, C):
    if(key in se):
      ok = False
  
  # おける時は、集合に登録
  if(ok):
    for key in block(R, C):
      se.add(key)

# ブロックの個数
print(len(se)//4)

補足

関係するリンク(参考文献など)

筆者について

その他

  • 間違いを含んでいる可能性があります.
  • 方針と言いつつ,方針ではない感想などを書いている可能性があります.
  • A問題から解説がだんだん省略されます.
  • コードに書かれている解説の文言と,その上に書いてある解説の文言を変えている場合があります.

最後に一言

  • D問題見てすらいない。
  • 承諾侍
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?