We participated in AtCoder Beginner Contest 171. I was able to solve problems A to C. I will write down my thoughts at that time.
A - Rainy Season Returns the number of consecutive Rs from a character string of length 3. I pushed it.
S = input()
if 'RRR' in S:
print(3)
elif 'RR' in S:
print(2)
elif 'R' in S:
print(1)
else:
print(0)
B - Making Triangle A length list L of N bars is given. Select three of them and return the number of combinations that meet the conditions.
I brute-forced the combinations and used itertools.combinations for that. To find out how to use it during the competition ... itertools.combinations(N,r) Returns a list of r combinations to choose from list N. Example: combinations (range (5), 3) → (0,1,2), (0,1,3), (0,1,4), (0,2,3), (0,2,4) ), (0,3,4), (1,2,3), (1,2,4), (1,3,4), (2,3,4)
import itertools
N = int(input())
L = list(map(int, input().split()))
count = 0
for i in itertools.combinations(range(N), 3):
Lx = sorted([L[i[0]], L[i[1]], L[i[2]]])
if not (Lx[0] == Lx[1] or Lx[1] == Lx[2] or Lx[2] == Lx[0]):
if Lx[0] + Lx[1] > Lx[2] :
count += 1
print(count)
C - Walking Takahashi $ X, K, and D $ are the initial position, the number of movements, and the movement distance, respectively. The movement direction can be selected from positive or negative, and it is closest to 0.
Let $ i $ be the number of positive moves and the final position be $ d $
X + iD - \left((K-i)D\right) = d\\
X/D + (2i - K) = d/D\\
\frac{X/D - K}{2} + i = d/2D\\
The absolute value of $ d $ is close to $ 0 $ ⇒ $ \ frac {X / D --K} {2} + i ; (i = 0,1, \ ldots, K) Find $ i $ where $ is closest to $ 0 $. ⇒ Put $ \ frac {X / D-K} {2} = {\ rm dist} $ and put it
if dist > 0
⇒ i = 0
else if dist + K < 0
⇒ i = K
else
⇒ i = -round(dist)
Meet
X, K, D = list(map(int, input().split()))
dist= (X/D-K)/2
if -dist > K :
i = K
elif dist > 0 :
i = 0
else :
i = -round(dist)
print(abs(X + i*D - ((K-i)*D)))
I haven't done it since the D problem ...
Recommended Posts