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?

AtCoderをPythonで取り組んだ振り返り(ABC431 A~C)

Posted at

どうもこんにちは!

今週のコンテストはCまで完答、振り返りもCまで。
ひさびさの3完、しかも15分ほどで終われたのは過去にないスピードだったと思います。
Dはとりあえず、ポイントが頭の方が高いパーツと体の方が高いパーツを分別して、これで頭の方が重いなら再帰を使ってパーツを体に変更して最高となるポイントを探索すればいけるのではと考えたものの、実装しきれずタイムアップとなりました。

問題と公式の解説のリンク

問題は以下のリンクから。

A - Robot Balance -

問題

2つの整数h,bが与えられ、もしh>bならその差を、そうでなければ0を出力する問題。

考え方とコード

if文使って判定しました。

h,b = map(int,input().split())
print(0 if b >= h else h-b)

B - Robot Weight -

問題

重さxのロボがあるとします。加えて重さがそれぞれ決まっているn個のパーツがあります。
クエリでパーツの着脱の指定をq回されるので、クエリを1個実行するたびにロボの重さを出力するという問題。

考え方とコード

クエリで指定されたパーツの着脱状態を保持するリストを作って、付いているときは外して重さをパーツ分減らす、外れているときは付けて重さをパーツ分増やして出力としました。

x = int(input())
n = int(input())
w = [int(x) for x in input().split()]
q = int(input())

t = [False] * n
for _ in range(q):
    p = int(input()) - 1
    if t[p] == False:
        t[p] = True
        x += w[p]
    else:
        t[p] = False
        x -= w[p]
    print(x)

C - Robot Factory -

問題

頭のパーツn個と体のパーツm個が与えられます。このパーツにはそれぞれ重さが与えられています。
頭のパーツ1個と体のパーツ1個を組み合わせてロボットを作るとしたとき、与えられたk個分を作れるか判定する問題。
組み合わせの条件は頭のパーツの重さが体のパーツの重さ以下であることと、1度使ったパーツを再利用できないことです。

考え方とコード

頭のパーツのうち、軽い方からk個分でロボットを作れるかを確認すればよいです。
頭のパーツと体のパーツを軽い順にソートして、それぞれ軽いパーツから組み合わせを見ていくようにして判定しました。

n,m,k = map(int,input().split())
h = [int(x) for x in input().split()]
h.sort()
b = [int(x) for x in input().split()]
b.sort()

comp = 0
idx_h,idx_b = 0, 0
while idx_b < m:
    if h[idx_h] <= b[idx_b]:
        comp += 1
        idx_h += 1
        idx_b += 1
        if comp == k:
            break
    else:
        idx_b += 1

print("Yes" if comp == k else "No")

ではでは。

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?