I was able to finish the Bachacon in the third place, probably because I was feeling better. However, the last question was unproven and I passed it in a slightly different way from the answer, so I would like to review the answer firmly.
Carry up. I didn't use math.ceil because of the error. I feel like I won't use math.ceil in the future.
answerA.py
a,b=map(int,input().split())
print(-((-a-b)//2))
Just think about the size of the sorted one. I was casually comparing the size of the list, but ** the size comparison of the list works the same as the size comparison of the character string **, isn't it?
answerB.py
s,t=sorted(list(input())),sorted(list(input()),reverse=True)
print("Yes" if s<t else "No")
Also, I was surprised because I had a problem using the groupby function. Old problem Isn't it too much of a problem using the groupby function? ?? Divide into groups by number and remove all if there are fewer than that number, and if there are more than that number, remove until they are the same number.
answerC.py
n=int(input())
a=sorted(list(map(int,input().split())))
def groupby():
global a
a2=[[a[0],1]]
for i in range(1,len(a)):
if a2[-1][0]==a[i]:
a2[-1][1]+=1
else:
a2.append([a[i],1])
return a2
b=groupby()
ans=0
for i in b:
if i[0]>i[1]:
ans+=i[1]
else:
ans+=(i[1]-i[0])
print(ans)
First, consider ** what kind of behavior the robot will show with F and T, respectively **. First of all, F is easy. When F continues for k pieces, it will advance by k pieces in the direction facing at that time. On the contrary, what if there are k consecutive T's? At this time, how far you go does not change, but the direction changes. Note that there are only four directions, and if the directions are different by 180 degrees, it can be said that the positive and negative directions are different in the same direction. In other words, ** in the end, it turns out that there are only two directions **, and if you draw a little figure and think about it, ** the direction does not change when k is even, and the direction changes when k is odd **. understood. From the above, we can see how far each continuous F travels in either the x-axis or the y-axis. Also, since the x-axis direction and the y-axis direction can be considered independently, we will consider ** what happens to the x-axis direction ** below. The next thing to consider is that when the robot advances $ f_1, f_2,…, f_k $ k times in the x-axis direction, decide whether to make each positive or negative ($ 2 ^ k $ way) and just add up the total. The question is whether it can be x. Here, I sort $ f_1, f_2,…, f_k $ in descending order and select positive or negative so that they are as close to x as possible in order from the largest f. However, this was done unproven, so we need to do it in a provable way (I don't know why it was done this way). Here, we had to use ** DP **, noting that it was $ 2 ^ k $. That is, from-(absolute value of $ f_1, f_2,…, f_k $) to the absolute value of $ f_1, f_2,…, f_k $, select $ f_1, f_2,…, f_k $ in order and DP You just have to do. It can be said that the robot can reach the coordinates (x, y) if it can be set to x as described above and the y coordinate can also be set to y. Also, the second code was the fastest PyPy code.
answerD.py
from sys import exit
s=input()
x,y=map(int,input().split())
def groupby(a):
a2=[[a[0],1]]
for i in range(1,len(a)):
if a2[-1][0]==a[i]:
a2[-1][1]+=1
else:
a2.append([a[i],1])
return a2
s=groupby(s)
a,b=[],[]
turn=True
#If straight at the beginning
if s[0][0]=="F":
x-=s[0][1]
s.pop(0)
for i in s:
if i[0]=="F":
if turn:
a.append(i[1])
else:
b.append(i[1])
else:
if i[1]%2==1:
turn=not turn
a.sort(reverse=True)
b.sort(reverse=True)
for i in a:
if x>0:
x-=i
else:
x+=i
for i in b:
if y>0:
y-=i
else:
y+=i
if x!=0 or y!=0:
print("No")
else:
print("Yes")
answerD_faster.py
def main():
from sys import exit
s=input()
x,y=map(int,input().split())
def groupby(a):
a2=[[a[0],1]]
for i in range(1,len(a)):
if a2[-1][0]==a[i]:
a2[-1][1]+=1
else:
a2.append([a[i],1])
return a2
s=groupby(s)
a,b=[],[]
turn=True
#If straight at the beginning
if s[0][0]=="F":
x-=s[0][1]
s.pop(0)
for i in s:
if i[0]=="F":
if turn:
a.append(-i[1])
else:
b.append(-i[1])
elif i[1]%2==1:
turn=not turn
a.sort()
b.sort()
for i in a:
x=abs(x)-abs(i)
if x!=0:
print("No")
exit()
for i in b:
y=abs(y)-abs(i)
if y!=0:
print("No")
exit()
print("Yes")
main()
Recommended Posts