[PYTHON] AtCoder Beginner Contest 082 Review of past questions

Time required

スクリーンショット 2020-03-26 19.41.37.png

Impressions

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.

Problem A

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))

B problem

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")

C problem

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)

D problem

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

AtCoder Beginner Contest 102 Review of past questions
AtCoder Beginner Contest 072 Review of past questions
AtCoder Beginner Contest 062 Review of past questions
AtCoder Beginner Contest 113 Review of past questions
AtCoder Beginner Contest 074 Review of past questions
AtCoder Beginner Contest 051 Review of past questions
AtCoder Beginner Contest 127 Review of past questions
AtCoder Beginner Contest 119 Review of past questions
AtCoder Beginner Contest 054 Review of past questions
AtCoder Beginner Contest 117 Review of past questions
AtCoder Beginner Contest 105 Review of past questions
AtCoder Beginner Contest 112 Review of past questions
AtCoder Beginner Contest 076 Review of past questions
AtCoder Beginner Contest 089 Review of past questions
AtCoder Beginner Contest 069 Review of past questions
AtCoder Beginner Contest 079 Review of past questions
AtCoder Beginner Contest 056 Review of past questions
AtCoder Beginner Contest 087 Review of past questions
AtCoder Beginner Contest 067 Review of past questions
AtCoder Beginner Contest 093 Review of past questions
AtCoder Beginner Contest 046 Review of past questions
AtCoder Beginner Contest 123 Review of past questions
AtCoder Beginner Contest 049 Review of past questions
AtCoder Beginner Contest 078 Review of past questions
AtCoder Beginner Contest 081 Review of past questions
AtCoder Beginner Contest 047 Review of past questions
AtCoder Beginner Contest 060 Review of past questions
AtCoder Beginner Contest 104 Review of past questions
AtCoder Beginner Contest 057 Review of past questions
AtCoder Beginner Contest 121 Review of past questions
AtCoder Beginner Contest 126 Review of past questions
AtCoder Beginner Contest 090 Review of past questions
AtCoder Beginner Contest 061 Review of past questions
AtCoder Beginner Contest 059 Review of past questions
AtCoder Beginner Contest 044 Review of past questions
AtCoder Beginner Contest 083 Review of past questions
AtCoder Beginner Contest 048 Review of past questions
AtCoder Beginner Contest 116 Review of past questions
AtCoder Beginner Contest 088 Review of past questions
AtCoder Beginner Contest 092 Review of past questions
AtCoder Beginner Contest 099 Review of past questions
AtCoder Beginner Contest 065 Review of past questions
AtCoder Beginner Contest 053 Review of past questions
AtCoder Beginner Contest 094 Review of past questions
AtCoder Beginner Contest 063 Review of past questions
AtCoder Beginner Contest 107 Review of past questions
AtCoder Beginner Contest 064 Review of past questions
AtCoder Beginner Contest 082 Review of past questions
AtCoder Beginner Contest 084 Review of past questions
AtCoder Beginner Contest 068 Review of past questions
AtCoder Beginner Contest 058 Review of past questions
AtCoder Beginner Contest 043 Review of past questions
AtCoder Beginner Contest 098 Review of past questions
AtCoder Beginner Contest 114 Review of past questions
AtCoder Beginner Contest 045 Review of past questions
AtCoder Beginner Contest 120 Review of past questions
AtCoder Beginner Contest 108 Review of past questions
AtCoder Beginner Contest 106 Review of past questions
AtCoder Beginner Contest 122 Review of past questions
AtCoder Beginner Contest 125 Review of past questions
AtCoder Beginner Contest 109 Review of past questions