1
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?

ABC379をPythonで(A~E)

Posted at

トヨタ自動車プログラミングコンテスト2024#11(AtCoder Beginner Contest 379)の解答等の速報的まとめ

A問題

入力を受け取って並び替える

A
a,b,c = input()
print(b + c + a, c + a + b)

B問題

連続でk個Oがあったとき+1する

B
n, k = map(int, input().split())
s = input()

ans = 0
cnt = 0
for s_i in s:
    if s_i == "O":
        cnt += 1
    else:
        cnt = 0

    if cnt == k:
        ans += 1
        cnt = 0

print(ans)

C問題

各$x_i$でソートした後、隣接した$x_i, x_{i+1}$間を計算すればいい

C
def calc(s):
    return s * (s + 1) // 2 if s > 0 else 0


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


data = [(x_i, a_i) for x_i, a_i in zip(x, a)]
data.sort()
if sum(a) != n:
    exit(print(-1))

if data[-1][0] < n:
    data.append((n, 0))

last = 1
cnt = 0
ans = 0
for x_i, a_i in data:
    if x_i - last > cnt:
        exit(print(-1))
    ans += (x_i - last) * cnt - calc(x_i - last)
    cnt = cnt - (x_i - last) + a_i
    last = x_i

print(ans)

D問題

鉢植えの高さを植えた時間を基準に見ればheapqでできる
(入れた順に先頭から出すだけなのでdequeでいい)

D
from heapq import heappop, heappush

q = list()
time = 0
last = 0
for _ in range(int(input())):
    com = list(map(int, input().split()))
    if com[0] == 1:
        heappush(q, time)
    elif com[0] == 2:
        time += com[1]
    else:
        ans = 0
        while q and q[0] <= time - com[1]:
            ans += 1
            heappop(q)

        print(ans)

E問題

sが小さければ以下のようにすればいい

demo
n = int(input())
s = input()

ans = 0
last = 0
for i, s_i in enumerate(s, 1):
    diff = diff * 10 + int(s_i) * i
    ans += diff

print(ans)

ただこのままだと、問題の設定でオーバーフローするので各桁ごとに計算する。

E
n = int(input())
s = input()

accu = [0] * n
for i, s_i in enumerate(s):
    accu[i] += int(s_i) * (i + 1)

for i in range(n - 1):
    accu[i + 1] += accu[i]

lst = []
d = 0
for a_i in accu[::-1]:
    d += a_i
    lst.append(str(d % 10))
    d //= 10

ans = "".join(lst[::-1])
if d:
    ans = str(d) + ans

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