0
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?

[ABC464] ABC 464のA~C問題をPythonで解説

0
Posted at

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

A問題

  • .count("E or W") で比較しようかな。
A.py
"""
<方針>
- `.count("E or W")` で比較しようかな。
"""
# 入力
S = input()

# カウントする
east = S.count("E")
west = S.count("W")

# 比較して出力
if(east < west):print("West")
if(west < east):print("East")

B問題

  • 回転させて後ろが全部白だったら取り除けば良さそう。
B.py
"""
<方針>
- 回転させて後ろが全部白だったら取り除けば良さそう。
"""
# 入力
H, W = map(int, input().split())
CC = [list(input()) for _ in range(H)]

# 4回回転させる
for _ in range(4):
  CC = [C for C in zip(*reversed(CC))] # 回転
  # 後ろが全部白だったら取り除く
  while True:
    C = CC.pop()
    if(any([(c == "#") for c in C])):
      CC.append(C)
      break

# 出力
for C in CC:
  print("".join(C))

C問題

方針

  • 今どの色がいくつあるかを辞書で持っとく。
  • その日の色の変更点の一覧をパッと出せるようにしておく。

前提

  • C問題あたりで,TLEになる人は,制約条件を見る癖をつけよう.
  • AB問題では,基本的に制約条件を見ずにやっても解ける.
  • しかし,C問題以降では,制約条件を見ないと必ずTLEすると思っても良い.
  • 詳しい話は私の352回の記事C問題の解説に記したので,是非参照してほしい.
C.py
"""
<方針>
- 今どの色がいくつあるかを辞書で持っとく。
- その日の色の変更点の一覧をパッと出せるようにしておく。
"""
from collections import defaultdict

N, M = map(int, input().split())
ADB = [list(map(int, input().split())) for _ in range(N)]

ans = 0
diColor = defaultdict(int)
for a, _, _ in ADB:
  diColor[a] += 1
  if(diColor[a] == 1):ans += 1

lsDate = [[] for _ in range(M)]
for a, d, b in ADB:
  lsDate[d-1].append([a, b])

for i in range(M):
  for a, b in lsDate[i]:
    diColor[a] -= 1
    if(diColor[a] == 0):ans -= 1
    diColor[b] += 1
    if(diColor[b] == 1):ans += 1
  print(ans)

補足

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

筆者について

その他

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

最後に一言

  • 研究室の後輩とペアプロしながらやりました。
0
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
0
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?