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?

AtCoder ABC411 振り返り(緑コーダーがPythonでABC問題)

Posted at

ABC411を振り返ります

今回はABCまで3問解答(1ミス)でした。久々に灰色パフォーマンスでした。
C問題でなかなか解法がわからず、時間がかかった上に1ミスしてしまいました。
前回(ABC410)はいい成績だったのですけど、安定して好成績を出すのは難しいですね...

A - Required Length

文字列長がL以上かどうかを判定する問題です。素直に解きました。

P = input()
L = int(input())
# 文字列長がL以上かどうかを判定する
print("Yes" if len(P) >= L else "No")

B - Distance Table

スタート地点を固定して、そこから後ろの点への距離を計算します。

N = int(input())
D = list(map(int, input().split()))
D.insert(0, 0)

# スタート地点をiに固定して、そこから後ろの点への距離を計算する
for i in range(N - 1):
    distance = 0
    answers = []
    for j in range(i + 1, N):
        distance += D[j]
        answers.append(distance)
    print(*answers)

C - Black Intervals

最初は「Union-Findを使うのか?」とか、色々迷ってしまって誤答してしまいました。

その後、黒いマスが増えたとき/減ったときのエリア数の変化だけに注目すれば良いことに気づき、解けました。

白いマス=0, 黒いマス=1として考えます。真ん中が0→1になったとき、両隣のマスによって、黒エリアの数がどう変わるかを考えます。

両隣が0であれば、エリア数が1増えます。
0 0 0 → 0 1 0

どちらか1つが1であれば、エリア数は変わりません。(既存エリアにくっつくので)
1 0 0 → 1 1 0

両隣が1であれば、エリア数は1減ります。(既存の2エリアを1つに統合するので)
1 0 1 → 1 1 1

1→0になったときも、同じようにエリア数の増減を考えます。

N, Q = map(int, input().split())
A = list(map(int, input().split()))

boxes = [0] * (N + 2)
area_count = 0

for i in range(len(A)):
    index = A[i]
    new_value = 1 if boxes[index] == 0 else 0
    boxes[index] = new_value

    # 黒いマスが増えたとき
    if boxes[index] == 1:
        # 両隣が0であれば、エリア数が1増える
        if boxes[index - 1] == 0 and boxes[index + 1] == 0:
            area_count += 1
        # 両隣が1であれば、エリア数が1減る
        elif boxes[index - 1] == 1 and boxes[index + 1] == 1:
            area_count -= 1

    # 黒いマスが減ったとき
    if boxes[index] == 0:
        # 両隣が1であれば、エリア数が1増える
        if boxes[index - 1] == 1 and boxes[index + 1] == 1:
            area_count += 1
        # 両隣が0であれば、エリア数が1減る
        if boxes[index - 1] == 0 and boxes[index + 1] == 0:
            area_count -= 1

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