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?

ABC361をPythonで(A~D)

Posted at

デンソークリエイトプログラミングコンテスト2024(AtCoder Beginner Contest 361)の回答まとめ

A問題

A
n, k, x = map(int, input().split())
a = list(map(int, input().split()))

b = a[:k] + [x] + a[k:]
print(*b)

B問題

B
a = list(map(int, input().split()))
b = list(map(int, input().split()))

def check(s, t):
  return s[0] < t[3] and s[1] < t[4] and s[2] < t[5]

if check(a, b) and check(b, a):
  print("Yes")
else:
  print("No")

C問題

C
n, k = map(int, input().split())
a = list(map(int, input().split()))

if k == n - 1:
    print(0)
    exit()

a.sort()
ans = float("inf")
for i in range(n):
    j = i + n - k - 1
    if n <= j:
        break
    ans = min(ans, a[j] - a[i])

print(ans)

D問題

D
from collections import deque

n = int(input())
s = list(input()) + [".", "."]
t = list(input()) + [".", "."]

if sorted(s) != sorted(t):
    print(-1)
    exit()

d = dict()
d[tuple(s)] = 0
q = deque()
q.append([0, s, n])

while q:
    c, a, space = q.popleft()
    if d[tuple(a)] < c:
        continue
    
    for i in range(n + 1):
        if i in {space, space + 1}:
            continue
        if i < space:
            nxt = a[:i] + a[space:space + 2] + a[i + 2:space] + a[i:i + 2] + a[space + 2:]
        else:
            nxt = a[:space] + a[i:i + 2] + a[space + 2:i] + a[space:space + 2] + a[i + 2:]
        if tuple(nxt) in d and d[tuple(nxt)] <= c + 1 or len(nxt) > n + 2:
            continue
        else:
            d[tuple(nxt)] = c + 1
            q.append([c + 1, nxt, i])
        
print(-1 if tuple(t) not in d else d[tuple(t)])
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?