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?

ABC477のPython解答(A~D)

0
Posted at

UNICORNプログラミングコンテスト2026(AtCoder Beginner Contest 477)の解答等の速報的まとめ

A問題

変換規則の辞書を作成

A
traffic = {"B":"Y", "Y":"R", "R":"B"}

print(traffic[input()])

B問題

ソートして前後との差を確認

B
n, d = map(int, input().split())
x = list(map(int, input().split()))

lst = sorted(x)

flg = dict()
for i, l_i in enumerate(lst):
    flg[l_i] = True
    if i > 0:
        if l_i - lst[i - 1] < d:
            flg[l_i] = False
    if i + 1 < n:
        if lst[i + 1] - l_i < d:
            flg[l_i] = False

ans = list()
for i, x_i in enumerate(x, 1):
    if flg[x_i]:
        ans.append(i)

print(len(ans))
print(*ans)

C問題

先に$S$の中で$T$と一致する部分の開始位置を調べる
2分探索で

C
from bisect import bisect_left

def find_ge(a, x):   # 探索したい数値以上のうち最小の数値を探索
    i = bisect_left(a, x)
    if i != len(a):
        return a[i]
    return 10 ** 18

q = int(input())
s = input()
t = input()

lst = list()
for i in range(len(s)):
    if s[i:i + len(t)] == t:
        lst.append(i)

for _ in range(q):
    l, r = map(lambda x:int(x) - 1, input().split())
    target = find_ge(lst, l)
    print("Yes" if target + len(t) - 1 <= r else "No")

D問題

タイルを貼ったときにその下の色を記録する
最後にタイルが貼ってあるときは記録している色
そうでないときは最後に更新した色

タイルを剝がした後色を更新する前にタイルを貼りなおしたりクエリが終わったときの扱いに気を付ける

なお、コンテスト中にデバッグが間に合わず5分ほどタイムオーバー

D
n, q = map(int, input().split())
query = list()

for _ in range(q):
    a, b = input().split()
    a_ = int(a)
    if a_ == 1:
        b = int(b) - 1
    query.append([a_, b])

last_color = ["a"] * n
last_tile = [-1] * n
tile_set = set()

color = "a"
last_query_2 = -1
for i in range(q):
    com, num = query[i]
    if com == 1:
        if num in tile_set:
            tile_set.discard(num)
            last_tile[num] = i
        else:
            if last_tile[num] < last_query_2:
                last_color[num] = color
            tile_set.add(num)
    else:
        color = num
        last_query_2 = i

ans = list()
for i in range(n):
    if i in tile_set or last_tile[i] >= last_query_2:
        ans.append(last_color[i])
    else:
        ans.append(color)

print("".join(ans))
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?