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?

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

Posted at

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

A問題

  • feet に 12 をかけるだけ。
A.py
"""
<方針>
- フィードに `12` をかけるだけ。
"""
# 入力
A, B = map(int, input().split())

# 計算
ans = 12*A + B

# 出力
print(ans)

B問題

  • 呼ばれた数字を集合 se に入れておく。
  • 行ごとに数字を見ていき、se に入ってる個数をカウントし、cnts に記録する
  • cnts の最大値を出力すれば良い。
B.py
"""
<方針>
- 呼ばれた数字を集合 `se` に入れておく。
- 行ごとに数字を見ていき、`se` に入ってる個数をカウントし、`cnts` に記録する
- `cnts` の最大値を出力すれば良い。
"""
# 入力
H, W, N = map(int, input().split())
AA = [list(map(int, input().split())) for _ in range(H)]
B = [int(input()) for _ in range(N)]

se = set(B) # 集合の準備
cnts = [] # 全体の個数リスト

# カウント
for i in range(H):
  cnt = 0 # 今回の行の個数
  for j in range(W):
    # 集合にあるかどうか
    if(AA[i][j] in se):
      cnt += 1
  
  # 全体の個数リスト追加
  cnts.append(cnt)

# 出力
ans = max(cnts)
print(ans)

C問題

方針

  • 軽くて(W が小さく)、力が小さい(P が小さい)やつからソリに乗ってくれ。頼む。

前提

  • C問題あたりで,TLEになる人は,制約条件を見る癖をつけよう.
  • AB問題では,基本的に制約条件を見ずにやっても解ける.
  • しかし,C問題以降では,制約条件を見ないと必ずTLEすると思っても良い.
  • 詳しい話は私の352回の記事C問題の解説に記したので,是非参照してほしい.
C.py
"""
<方針>
- 軽くて(`W` が小さく)、力が小さい(`P`  が小さい)やつからソリに乗ってくれ。頼む。
"""
# 入力
T = int(input())

for _ in range(T):
  N = int(input())
  WP = [list(map(int, input().split())) for _ in range(N)]
  WP.sort(key=lambda x: sum(x))
  sumP = sum([p for _, p in WP])
  
  acc = 0
  for i, (w, p) in enumerate(WP):
    acc += w+p
    if(sumP < acc):
      break
  print(i)

補足

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

筆者について

その他

  • 間違いを含んでいる可能性があります.
  • 方針と言いつつ,方針ではない感想などを書いている可能性があります.
  • 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?