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?

ABC478のPython解答(A~D, F)

0
Posted at

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

A問題

割り算とあまりをいい感じに

A
n, m = map(int, input().split())

a = m // n
b = m % n

for i in range(n):
    if i < b:
        print(a + 1)
    else:
        print(a)

B問題

全パターン計算

B
n, v = map(int, input().split())
w = list(map(int, input().split()))

ans = 0
for i in range(n):
    for j in range(i + 1, n):
        for k in range(j + 1, n):
            if i + 1 + j + 1 + k + 1 <= v:
                ans = max(ans, w[i] + w[j] + w[k])

print(ans)

C問題

SortedListを利用して全パターン計算

C
from sortedcontainers import SortedList

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

left = [True] * n
for i in range(1, n):
    if a[i - 1] > a[i] or not left[i - 1]:
        left[i] = False

right = [True] * n
for i in range(n - 2, -1, -1):
    if a[i] > a[i + 1] or not right[i + 1]:
        right[i] = False

s = SortedList(a[:k])
left_i, right_i = 0, k - 1
while right_i < n:
    if left_i == 0:
        flg1 = True
    elif left[left_i - 1] and a[left_i - 1] <= s[0]:
        flg1 = True
    else:
        flg1 = False

    if right_i == n - 1:
        flg2 = True
    elif right[right_i + 1] and s[k - 1] <=  a[right_i + 1]:
        flg2 = True
    else:
        flg2 = False

    if flg1 and flg2:
        print("Yes")
        exit()

    s.discard(a[left_i])
    if right_i + 1 < n:
        s.add(a[right_i + 1])
    left_i += 1
    right_i += 1

print("No")

D問題

$x$ごとに範囲を結合したあと累積和で計算

D
n, q = map(int, input().split())

d = dict()
for _ in range(q):
    l, r, x = map(lambda x:int(x) - 1, input().split())
    if x not in d:
        d[x] = list()
    d[x].append([l, r + 1])

lst = [0] * (n + 10)
for val in d.values():
    a = sorted(val)
    b = list()
    for l_i, r_i in a:
        if not b or b[-1][1] < l_i:
            b.append([l_i, r_i])
        else:
            b[-1][1] = max(r_i, b[-1][1])

    for l_i, r_i in b:
        lst[l_i] += 1
        lst[r_i] -= 1

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

print(*lst[:n])

E問題

有向グラフに見立てて求めるだろうと思ったが、そこから先がわからずスキップ

F問題

パターン数は各頂点から前にたどりそれを超えるまでの個数の積。

実装は後ろから見て、スタックに入っているもののうち今の数未満のものを取り出して間の個数をかける

なお45秒提出に間に合わなかった。

F
mod = 998244353

n = int(input())
q = list(map(int, input().split()))

ans = 1
lst = list()
for i, q_i in enumerate(q[::-1]):
    while lst and lst[-1][0] < q_i:
        _, turn = lst.pop()
        ans *= (i - turn)
        ans %= mod
    lst.append([q_i, i])

for _, t_i in lst[:-1]:
    ans *= (lst[-1][1] - t_i)
    ans %= mod

print(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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?