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

初参加してないけどpyhtonでABC431をやってみた。

Last updated at Posted at 2025-11-21

1. はじめに

こんにちは、荻原です。
atcoderの初参加を432にしたかったのですが、間に合いませんでした。
そこで、ABC431の問題で練習しました。
それの記録です。

この記事は、B問題まで書いてあります。
C問題以降は、別の方のを見てください。

2. A Robot Balance

条件から答えが負になることはありえないので、
答えが負にならないよう,
マイナスの数を0にしながら減算をします。

def main():
  H,B = map(int, input().split()) # 入力
  ans = "" # 答えの宣言
  
  if(H > B):
    ans = H - B # 計算
  else:
    ans = 0 # 0以下は0にする
    
  print(ans) # 出力

  
if __name__ == "__main__":
  main()

3. B Robot Weight

A問題と比べて、複雑さが増しています。
主に、for、配列、そしてbool型の扱い方が、
わかっていれば解けると思います。

def main():
  X = int(input()) # 入力
  N = int(input())
  W = list(map(int, input().split()))
  Q = int(input())
  b = [False] * N # 部品をつけていないとしてる

  for _ in range(Q): # カウントを使わないfor文
    P = int(input()) - 1
    b[P] = not b[P] # bool反転
    
    ans = X
    for i in range(N): # 計算
      if b[i]:
        ans += W[i]
        
    print(ans) # 出力

if __name__ == "__main__":
  main()

4.おわりに

かなり短くなったけど、今日夜のABC433の記事も早めに書くので、頑張ります。

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