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?

[ABC467] ABC 467のA~C問題をPythonで解説

1
Posted at

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

A問題

  • 割り算は怖いので掛け算にしよう
A.py
"""
<方針>
- 割り算は怖いので掛け算にしよう
"""
# 入力
H, W = map(int, input().split())

# H*Hを右辺へ移項
# cmをmに変換するため、100*100を左辺に追加
if(W*100*100 >= 25*H*H):
  # 出力
  print("Yes")
else:
  print("No")

B問題

  • keep の時だけ差分を記録すれば良さそう。
B.py
"""
<方針>
- `keep` の時だけ差分を記録すれば良さそう。
"""
ans = 0
N = int(input())
for _ in range(N):
  A, B, S = map(str, input().split())
  A = int(A)
  B = int(B)
  if(S == "keep"):
    ans += (B - A)

print(ans)

C問題

方針

  • 0 1 しかないので、xor で考えれば良い。
  • A[0] の値が 0 1 の2パターン試して、小さい方を選択すれば良い。

前提

  • C問題あたりで,TLEになる人は,制約条件を見る癖をつけよう.
  • AB問題では,基本的に制約条件を見ずにやっても解ける.
  • しかし,C問題以降では,制約条件を見ないと必ずTLEすると思っても良い.
  • 詳しい話は私の352回の記事C問題の解説に記したので,是非参照してほしい.
C.py
"""
<方針>
- `0` `1` しかないので、`xor` で考えれば良い。
- `A[0]` の値が `0` `1` の2パターン試して、小さい方を選択すれば良い。
"""
N, _ = map(int, input().split())
rawA = list(map(int, input().split()))
B = list(map(int, input().split()))

ans = float("inf")
for firstBit in range(2):
  # 準備
  cnt = 0
  A = rawA[:]
  
  # A[0]
  if not (A[0] == firstBit):
    cnt += 1
    A[0] = not A[0]
  
  # A[i]
  for i in range(N-1):
    if((A[i] != A[i+1]) != B[i]):
      cnt += 1
      A[i+1] = not A[i+1]
  
  ans = min(ans, cnt)
  
print(ans)

補足

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

筆者について

その他

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

最後に一言

  • 今月中に取ろうと思ってた資格ですが、どうも無理そうです。
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?