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 ABC 465 振り返り (PythonでABC問題)

0
Posted at

ABC465を振り返ります

ABCまで3問解答でした。

前2回はUnratedで様子見参加しており、そろそろ慣れてきたので、今回はRating参加にしてみました。

C問題までは結構早く解けたので手応えあったんですけども、Dは初見のLCAを使う問題だったので、解けずでした。

Ratingはありがたいことに微増でした。
今後も機会があれば緑色復帰を目指して、Rating参加していきたいところです。

A - Supermajority

問題で与えられた式は

A > B * 2 / 3

ですが、これだと割り算の誤差が出そうな気がしたので、

A * 3 > B * 2

で判定しました。

解説を見ると、別に割り算でも解けるらしいです。

A, B = map(int, input().split())

if A * 3 > B * 2:
    print("Yes")
else:
    print("No")

B - Parking 2

素直にif文で [L-R]の時間の料金と、それ以外の時間の料金を場合分けして解きました。

X, Y, L, R, A, B = map(int, input().split())

cost = 0
for i in range(A, B):
    if L <= i and i < R:
        cost += X
    else:
        cost += Y
print(cost)

C - Reverse Permutation

入力例1をよく見ると、

5のときは o 反転するので 5が先頭に来る
4のときは o 反転するので 4が末尾に来る
3のときは x なにもしない
2のときは o 反転するので 2が先頭に来る
...

となっているのがわかります。

つまり以下のような法則があるんじゃないかな...と思って、書いてみたら解けました。

  • o のときは反転 → 先頭か末尾に追加(先頭か末尾を入れ替え)
  • x のときはそのまま → 先頭か末尾に追加

ソースコードは配列の後ろから解いていますが、普通に前から処理していっても普通に解けるようです...

N = int(input())
S = input()
A = []

left = []
right = []
currentIsLeft = False

# 逆から探索
for i in range(N-1, -1, -1):
    # "o" なら位置を反転
    if S[i] == "o":
        currentIsLeft = not currentIsLeft
    
    # 左側 or 右側の配列に入れる
    if currentIsLeft:
        left.append(i + 1)
    else:
        right.append(i + 1)

# 右側の配列は反転し、左側とくっつける    
right.reverse()
answer = left + right

print(*answer)
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?