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?

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

Posted at

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

A問題

A.py
"""
<方針>
- `X*(2**Y)` です.
"""
# 入力
X, Y = map(int, input().split())

# 計算
ans = X*(2**Y)

# 出力
print(ans)

B問題

  • 到着時間と共に番号をリスト horses に保存する.
  • 到着時間でソートして,トップ3の番号を出力すれば良い.
B.py
"""
<方針>
- 到着時間と共に番号をリスト `horses` に保存する.
- 到着時間でソートして,トップ3の番号を出力すれば良い.
"""
# 入力
N = int(input())
T = list(map(int, input().split()))

# 順位を求める
horses = [(i+1, t) for (i, t) in enumerate(T)]
horses.sort(key=lambda x: x[1]) # 到着時間でソートする

# トップ3の番号を取り出す
top3 = list(map(lambda x: x[0], horses[:3]))

# 出力
print(*top3)

C問題

方針

  • x に対して,一つずつ毎回割っていては間に合わない.
  • そこで,x をインクリメントしたら,対象領域が横にずれるだけ.その差分を更新すれば良さそう.

前提

  • C問題あたりで,TLEになる人は,制約条件を見る癖をつけよう.
  • AB問題では,基本的に制約条件を見ずにやっても解ける.
  • しかし,C問題以降では,制約条件を見ないと必ずTLEすると思っても良い.
  • 詳しい話は私の352回の記事C問題の解説に記したので,是非参照してほしい.
C.py
"""
<方針>
- 各 `x` に対して,一つずつ毎回割っていては間に合わない.
- そこで,`x` をインクリメントしたら,対象領域が横にずれるだけ.その差分を更新すれば良さそう.
"""
T = int(input())
for _ in range(T):
  # 入力
  N, W = map(int, input().split())
  C = list(map(int, input().split()))
  
  # 2Wの周期でコストをまとめておく(CostBatch).
  cb = [0]*(2*W)
  for i, c in enumerate(C):
    cb[i%(2*W)] += c
  cb = cb + cb
  
  # 最小コストを考える.
  # 初期値は一番左を塗る
  ans = now = sum(cb[:W])
  # ずらしていく.
  for i in range((2*W) - 1):
    now += cb[i+W] - cb[i] 
    ans = min(ans, now)
  
  # 出力
  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?