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?

ABC470のPython解答(A~D)

0
Posted at

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

A問題

FizzBuzzのFizzだけ

A
for i in range(1, int(input()) + 1):
    if i % 3 == 0:
        print("Fizz")
    else:
        print(i)

B問題

一番多いものにする

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

d = dict()
for c_i in c:
    if c_i not in d:
        d[c_i] = 0
    d[c_i] += 1

maxi = max(d.values())
print(n - maxi)

C問題

1以上になっている要素を取得できるようにする
そうすると計算量が$Q$の定数倍に収まるので間に合う

C
n, q = map(int, input().split())

d = dict()
ans = 0
for _ in range(q):
    com = list(map(int, input().split()))
    if com[0] == 1:
        x = com[1]
        if x in d:
            target = d[x]
        else:
            target = 0
        ans ^= target
        target += 1
        ans ^= target
        d[x] = target
    else:
        keys = list(d.keys())
        for key in keys:
            target  = d[key]
            ans ^= target
            target -= 1
            if target:
                ans ^= target
                d[key] = target
            else:
                d.pop(key)
    print(ans)

D問題

2の操作を偶数回行うと元に戻るので
逆転しているかフラグをもって両方を操作する

D
n, q = map(int, input().split())
p = list(map(lambda x:int(x) - 1, input().split()))

lst = [0] * n
rev_flg = False
for i, p_i in enumerate(p):
    lst[p_i] = i

for _ in range(q):
    com = list(map(lambda x:int(x) - 1, input().split()))
    if com[0] == 0:
        x, y = com[1:]
        if rev_flg:
            a = lst[x]
            b = lst[y]
            lst[x], lst[y] = b, a
            p[a], p[b] = y, x
        else:
            a = p[x]
            b = p[y]
            p[x], p[y] = b, a
            lst[a], lst[b] = y, x
    else:
        rev_flg ^= True


if rev_flg:
    ans = [l_i + 1 for l_i in lst]
else:
    ans = [p_i + 1 for p_i in p]

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