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?

ABC484のPython解答(A~E)

0
Posted at

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

A問題

そのまま

A
b, r = map(int, input().split())
print((30 - b) * (30 - r))

B問題

そのまま

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

print(-1 if 9 not in a else min(a))

C問題

取り出すところと挿入するところの外側が一致しているか
取り出すのと挿入が同じ値かつそれ以外は一個ずれであること

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

left = 0
while left < n and a[left] == b[left]:
    left += 1

if left == n:
    print("Yes")
    exit()

right = n - 1
while right >= 0 and a[right] == b[right]:
    right -= 1

if a[left:right] == b[left + 1:right + 1] and a[right] == b[left]:
    print("Yes")
elif a[left + 1:right + 1] == b[left:right] and a[left] == b[right]:
    print("Yes")
else:
    print("No")

D問題

左から順に
その数の一つ前が同じ数だとパターン数は増加しない
その数より左に同じ数があったら、その個数分パターン数が減少

D
from collections import defaultdict

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

ans = 1
d = defaultdict(int)

for i, a_i in enumerate(a):
    if i and a[i - 1] == a_i:
        d[a_i] += 1
        continue
    ans += i - d[a_i]
    d[a_i] += 1

print(ans)

E問題

累積和をもとに条件を満たすものがあるか順に判定
座標圧縮してからBITを使えばメモリの問題なし

E
class BinaryIndexTree:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    def init(self, A):
        self.tree[1:] = A
        for i in range(1, self.size):
            if i + (i & -i) <= self.size:
                self.tree[i + (i & -i)] += self.tree[i]

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

    def range_sum(self, i, j):
        if i == 0:
            return self.sum(j)
        return self.sum(j) - self.sum(i - 1)

def compress(A):
    # 戻り値: 圧縮後の列, 値->rank, rank->元の値
    xs = sorted(set(A))
    mp = {x: i for i, x in enumerate(xs)}
    return [mp[a] for a in A], mp, xs


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

accu = [0]
for a_i in a:
    accu.append(accu[-1] + a_i)

INF = 10 ** 18 + 10
new_acc = [-INF]
for accu_i in accu:
    new_acc.append(accu_i - r)
    new_acc.append(accu_i)
    new_acc.append(accu_i - l)

lst, d, s = compress(new_acc)

tree = BinaryIndexTree(len(d) + 10)
tree.add(d[0], 1)

ans = 0
for accu_i in accu[1:]:
    min_i = d[accu_i - r]
    max_i = d[accu_i - l]

    ans += tree.range_sum(min_i, max_i)
    tree.add(d[accu_i], 1)

print(ans)

F問題

セグ木で$-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?