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?

ABC364をPythonで(A~D)

Posted at

日本レジストリサービス(JPRS)プログラミングコンテスト2024#2(AtCoder Beginner Contest 364)の解答等のまとめ

A問題

気持ち悪くなるフラグを用意して、直前がsweetか否かと合わせて判定していけばいい

A
n = int(input())
flag = False
flag2 = False
ans = "Yes"
for i in range(n):
    s = input()
    if flag2:
        ans = "No"
    elif s == "sweet":
        if flag:
            flag2 = True
        else:
            flag = True
    else:
        flag = False

print(ans)

B問題

問題文のまま動かせばいい
配列は0基準なのを間違えないように気を付ける

B
h, w = map(int, input().split())
i, j = map(lambda x:int(x) - 1, input().split())
c = [input() for _ in range(h)]
x = input()

array = {"L":(0, -1), "R":(0, 1), "U":(-1, 0), "D":(1, 0)}

for x_i in x:
    dx, dy = array[x_i]
    if 0 <= i + dx < h and 0 <= j + dy < w and c[i + dx][j + dy] == ".":
        i += dx
        j += dy

print(i + 1, j + 1)

C問題

より甘いものからいくつ食べられるかと
よりしょっぱいものからいくつ食べられるかを比較する

C
n, x, y = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

a.sort(reverse=True)
b.sort(reverse=True)

INF = float("inf")


def calc(a, x):
    x_a = 0
    for i, a_i in enumerate(a, 1):
        x_a += a_i
        if x_a > x:
            return i
    return len(a)


print(min(calc(a, x), calc(b, y), n))

D問題

$A$をソートして$b_j$から$d$離れた地点、すなわち$b_j - d$と$b_j + d$の間にある点の個数で2分探索をすればいい

(下のコードは$b$をソートしているけどしなくていい)

D
from bisect import bisect, bisect_left

n, q = map(int, input().split())
a = sorted(map(int, input().split()))
b = [list(map(int, input().split())) for _ in range(q)]

data = [(b_i, k_i, i) for i, (b_i, k_i) in enumerate(b)]
data.sort()
ans = [0] * q

for b_i, k_i, i in data:
    ok, ng = 3 * 10 ** 8, -1
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        mini = bisect_left(a, b_i - mid)
        maxi = bisect(a, b_i + mid)
        if maxi - mini >= k_i:
            ok = mid
        else:
            ng = mid

    ans[i] = ok

for ans_i in ans:
    print(ans_i)
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?