ABC172 - Atcoder/Python精進のための解説メモ

2020/11/26

ABC172 - A.Calc

AtCoder公式 | ABC172 - A.Calc

解答

a = int(input())
print(a + a ** 2 + a ** 3)

要点メモ

  • やるだけ

ABC172 - B.Minor Change

AtCoder公式 | ABC172 - B.Minor Change

解答

s = input()
t = input()
ans = 0
for s, t in zip(s, t):
    if s != t:
        ans += 1
print(ans)

要点メモ

  • 丁寧にやるだけ

ABC172 - C.Tsundoku

AtCoder公式 | ABC172 - C.Tsundoku

解答

n, m, k = map(int, input().split())
aaa = list(map(int, input().split()))
bbb = list(map(int, input().split()))
t, ans = sum(bbb), 0
j = m
for i in range(n + 1):
    while j > 0 and t > k:
        j -= 1
        t -= bbb[j]
    if t > k:
        break
    ans = max(ans, i + j)
    if i == n:
        break
    t += aaa[i]
print(ans)

別解

import bisect
n, m, k = map(int, input().split())
aaa = list(map(int, input().split()))
bbb = list(map(int, input().split()))
sum_a = [0] * (n + 1)
sum_b = [0] * (m + 1)

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

for j in range(m):
    sum_b[j + 1] = sum_b[j] + bbb[j]

ans = 0
for i in range(n + 1):
    if sum_a[i] > k:
        break
    tmp = bisect.bisect_right(sum_b, k - sum_a[i])
    ans = max(ans, i + tmp - 1)

print(ans)

要点メモ

  • 尺取り法を使う問題
  • 先に累積和を出しておいて、bisect_right使うほうが個人的には直感的で好き

ABC172 - D.

AtCoder公式 | ABC172 - D.

解答

まだ解いてません

要点メモ