ABC175 - Atcoder/Python精進のための解説メモ
2022/12/27
ABC175 - A.Rainy Season
AtCoder公式 | ABC175 - A.Rainy Season解答
s = input()
if s == 'RRR':
print(3)
elif s == 'RRS' or s == 'SRR':
print(2)
elif s == 'SSS':
print(0)
else:
print(1)
別解
ss = input()
tmp = 0
ans = 0
for s in ss:
if s == 'R':
tmp += 1
ans = tmp
else:
tmp = 0
print(ans)
要点解説メモ
- 全パターン分岐でもいけるくらいパターンが少ない
- 日数が増えても対応できるようにさっと書けるようにしておく
ABC175 - B.Making Triangle
AtCoder公式 | ABC175 - B.Making Triangle解答
n = int(input())
lll = list(map(int, input().split()))
lll.sort()
len_lll = len(lll)
ans = 0
for i in range(len_lll):
a = lll[i]
for j in range(i + 1, len_lll):
b = lll[j]
for k in range(j + 1, len_lll):
c = lll[k]
if a + b > c and a != b and b != c:
ans += 1
print(ans)
要点解説メモ
- 条件1:a, b, cがすべて異なること
- 条件2:三角形が成り立つ条件は、a+b>cが、a, b, cが入れ替わっても成り立つこと
- 全通り探索するのは大変なので、ソートしてから探索する
ABC175 - C.Walking Takahashi
AtCoder公式 | ABC175 - C.Walking Takahashi解答
x, k, d = map(int, input().split())
x = abs(x)
if k * d < x:
ans = x - k * d
else:
if (k - x // d) % 2 == 0:
ans = x % d
else:
ans = abs(x % d - d)
print(ans)
要点解説メモ
- 0を中心に行ったり来たりすることに気づく問題
- 0までたどり着かない場合も忘れずに
ABC175 - D.Moving Piece
AtCoder公式 | ABC175 - D.Moving Piece解答
n, k = map(int, input().split())
ppp = list(map(lambda x: int(x) - 1, input().split()))
ccc = list(map(int, input().split()))
ans = max(ccc)
for i in range(n):
tmp = 0
score = [0]
start = i
while ppp[start] != i:
start = ppp[start]
tmp += ccc[start]
score.append(tmp)
loop_score = score[-1] + ccc[i]
loop_len = len(score)
for j in range(loop_len):
if j <= k and j != 0:
loops = (k - j) // loop_len
thing = score[j] + max(0, loop_score) * loops
ans = max(ans, thing)
elif j == 0 and loop_len <= k:
thing = loop_score * k // loop_len
ans = max(ans, thing)
print(ans)
要点解説メモ
- 考察してPがすべて異なることから、ループすることにまず気づく
- スタート地点の選び方()×ループのどこまで合算するか×1周するかを場合分けして探索する
- PyPyじゃないと間に合わなそう
- 考察が結構大事になる問題なので、難しい気がする