LoginSignup
0
0

ABC326をPythonで(A~F)

Last updated at Posted at 2023-10-28

パナソニックグループ プログラミングコンテスト2023(AtCoder Beginner Contest 326)

A問題

A
x, y = map(int, input().split())
print("Yes" if x - 3 <= y <= x + 2 else "No")

B問題

全探索

B
n = int(input())
while True:
    x = str(n)
    if int(x[0]) * int(x[1]) == int(x[2]):
        print(n)
        exit()
    else:
        n += 1

C問題

尺取り

C
n, m = map(int, input().split())
a = sorted(map(int, input().split()))

right = 0
ans = 0
for left in range(n):
    while right < n and a[right] - a[left] < m:
        right += 1
    ans = max(ans, right - left)

print(ans)

D問題

DFSを使った全探索
ただし、全パターンを調べると間に合わないので足切りをしっかりする。

D
n = int(input())
R = input()
C = input()

ans = [["."] * n for _ in range(n)]
set_list = [[] for _ in range(n)]
d = {"A":["ABC", "ACB"], "B":["BAC", "BCA"], "C":["CAB", "CBA"]}

def DFS(i=0):
    if i == n:
        for j in range(n):
            if set(set_list[j]) == {"A", "B", "C"} and set_list[j][0] == C[j] and len(set_list[j]) == 3:
                pass
            else:
                return False

        print("Yes")
        for ans_i in ans:print("".join(ans_i))
        exit()

    for p_c in d[R[i]]:
        for c_i in combinations(range(n), 3):
            flag = True
            for j, p_j in zip(c_i, p_c):
                ans[i][j] = p_j
                set_list[j].append(p_j)
                if set_list[j][0] != C[j]:
                    flag = False
            if flag:
                DFS(i + 1)
            for j, p_j in zip(c_i, p_c):
                ans[i][j] = "."
                set_list[j].pop()

DFS()
print("No")

E問題

解説記事を参照

E
n = int(input())
a = [0] +  list(map(int, input().split()))
mod = 998244353
p = pow(n, -1, mod)

# dp[i] = i以降に稼ぐお金の期待値
# dp[i] = a[i] + sum(dp[i+1:]) / n
dp = [0] * (n + 1)
sums = 0
for i in range(n, -1, -1):
    dp[i] = sums * p % mod + a[i]
    dp[i] %= mod
    sums += dp[i]
    sums %= mod

print(dp[0])

F問題

y軸の操作は奇数番目に、x軸の操作は偶数番目にのみ行われる。
よってそれぞれでAの数字を+-で目標の数字にできるか調べる。

目標の数字にできるかは半分全列挙で求められる。

F
def merge(a):
    dic = {0:""}
    for a_i in a:
        new_d = dict()
        for key, val in dic.items():
            if key + a_i not in new_d:
                new_d[key + a_i] = val + "+"
            if key - a_i not in new_d:
                new_d[key - a_i] = val + "-"
        dic = new_d
    return dic

def solve(x, lst):
    S = merge(lst[:len(lst) // 2])
    T = merge(lst[len(lst) // 2:])
    for s_k, s_v in S.items():
        if x - s_k in T:
            return s_v + T[x - s_k]

    print("No")
    exit()

n, X, Y = map(int, input().split())
a = list(map(int, input().split()))

y_list = solve(Y, a[::2])
x_list = solve(X, a[1::2])

ans = []
now = [1, 0]
for i in range(n):
    if i % 2 == 0:
        if y_list[i // 2] == "+" and now == [1, 0]:
            ans.append("L")
            now = [0, 1]
        elif y_list[i // 2] == "-" and now == [-1, 0]:
            ans.append("L")
            now = [0, -1]
        elif y_list[i // 2] == "+" and now == [-1, 0]:
            ans.append("R")
            now = [0, 1]
        elif y_list[i // 2] == "-" and now == [1, 0]:
            ans.append("R")
            now = [0, -1]
    else:
        if x_list[i // 2] == "+" and now == [0, -1]:
            ans.append("L")
            now = [1, 0]
        elif x_list[i // 2] == "-" and now == [0, 1]:
            ans.append("L")
            now = [-1, 0]
        elif x_list[i // 2] == "+" and now == [0, 1]:
            ans.append("R")
            now = [1, 0]
        elif x_list[i // 2] == "-" and now == [0, -1]:
            ans.append("R")
            now = [-1, 0]

print("Yes")
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