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?

ABC485のPython解答(A~E)

0
Posted at

AtCoder Beginner Contest 485の解答等の速報的まとめ

A問題

両方プラスか両方マイナスならYes
かけてプラスなら上の条件を満たす

A
a,b = map(int, input().split())
print("Yes" if a * b >= 0 else "No")

B問題

1行すべて x ならそこの蛇口がダメになっている
そうでないなら、適当な一つを見ればダメな排水管がわかる

B
n, m = map(int, input().split())
s = [input() for _ in range(n)]

for i, s_i in enumerate(s, 1):
    if s_i == "x" * m:
        print("Faucet", i)
        exit()

for j, s_j in enumerate(s[0], 1):
    if s_j == "x":
        print("Drain", j)
        exit()

C問題

色ごとに分類
連番の端からの距離を見ればよい

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

t = [list() for _ in range(n + 10)]
for i, s_i in enumerate(s):
    t[s_i].append(i)

ans = [n] * n

for t_list in t:
    if not t_list:
        continue

    last = -n
    stack = list()
    for i, t_i in enumerate(t_list):
        if len(stack) == 0 and t_i != 0:
            last = t_i - 1
        stack.append(t_i)
        if (i + 1 < len(t_list) and t_list[i + 1] - t_i > 1) or (i == len(t_list) - 1 and t_i < n - 1):
            nxt = t_i + 1
            while stack:
                target = stack.pop()
                ans[target] = min(nxt - target, target - last)

    if stack:
        while stack:
            target = stack.pop()
            ans[target] = target - last

print(*ans)

D問題

まず右と左それぞれで$ABC$が残る最低限の幅を確認
そのあと、上の幅の中で$ABC$がとれる幅を尺取りで確認し、$ABC$のうち一番左のものの場所と左で最低限の幅の間の総和が答え

D
n = int(input())
s = input()

left = n
a_flg, b_flg, c_flg = False, False, False
for i, s_i in enumerate(s):
    a_flg |= s_i == "A"
    b_flg |= s_i == "B"
    c_flg |= s_i == "C"

    if a_flg and b_flg and c_flg:
        left = i
        break

right = 0
a_flg, b_flg, c_flg = False, False, False
for i in range(n - 1, -1, -1):
    s_i = s[i]
    a_flg |= s_i == "A"
    b_flg |= s_i == "B"
    c_flg |= s_i == "C"

    if a_flg and b_flg and c_flg:
        right = i
        break

if right <= left:
    print(0)
    exit()

a_last, b_last, c_last = -1, -1, -1
ans = 0
for i in range(left + 1, right):
    if s[i] == "A":
        a_last = max(i, a_last)
    if s[i] == "B":
        b_last = max(i, b_last)
    if s[i] == "C":
        c_last = max(i, c_last)

    mini = min(a_last, b_last, c_last)
    if mini > left:
        ans += mini - left

print(ans)

E問題

01BFS
色については、各地点で2色入ればいけない方向がなくなるのでその分だけ確認する

E
from collections import deque

n, m = map(int, input().split())
edge = [list() for _ in range(n)]
for _ in range(m):
    u, v, c = map(int, input().split())
    edge[u - 1].append([v - 1, c])

INF = float("inf")
check = [set() for _ in range(n)]
check[0].add(0)
q = deque()
q.append([0, 0])
cost = 0
while q:
    new_q = deque()
    cost += 1
    while q:
        now, col = q.popleft()
        if col not in check[now]:
            continue
        for to, c_to in edge[now]:
            if c_to == col:
                continue
            if c_to in check[to] or len(check[to]) > 2:
                continue
            check[to].add(c_to)
            new_q.append([to, c_to])
    
    if check[n - 1]:
        print(cost)
        exit()
    else:
        q = new_q

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