[PYTHON] AtCoder Beginner Contest 045 Review of past questions

Time required

スクリーンショット 2020-02-06 12.06.06.png

Impressions

The D problem worked (although it's poorly implemented). However, I made a mistake in the corner case due to the B and C problems and issued 3WA, so I would like to reflect on it and make use of it next time.

Problem A

Just find the area.

answerA.py


a=input()
b=input()
h=input()
print((int(a)+int(b))*int(h)//2)

B problem

You just have to do a simulation. Accurately grasp what kind of state it is in the while statement. Here, consider who the turn is and which alphabet card the person has.

answerB.py


s=[input() for i in range(3)]
l=[len(s[i]) for i in range(3)]
abc=[0,0,0]
now=0
while abc[now]!=l[now]:
    s_sub=s[now][abc[now]]
    abc[now]+=1
    now=(0 if s_sub=="a" else 1 if s_sub=="b" else 2)
print("A" if now==0 else "B" if now==1 else "C")

C problem

When the length of the character string is l, + can be entered in l-1 places, and since ** l is small enough, all $ 2 ^ {l-1} $ patterns ** can be considered. Masu (bit full search). Therefore, after that, consider addition in a state where the place where + enters is known. Here, looking at the character strings of numbers in order from the front, when + falls between the i-th and i + 1-th characters, consider the addition of the numbers on the left and the numbers on the right, and + is the i-th character. If it does not fit between the i + 1 characters, consider the number on the left side and the sum of the numbers on the right side as a ** character string . Therefore, write a bit full search, look between the numbers in order, and if + is entered in the jth (j = 0 → l-2), the number string (subst) saved at that time is an integer. In addition to ans_sub (don't forget to set the j + 1th number to subst), if + is not entered in the jth (j = 0 → l-2), add it to subst as a character string. , The code looks like this: ( bit I'm wondering whether to judge whether it is 1 in the full search code as `(i >> j) & 1``` or `(1 << j) & i```. I want to unify ... **)

Postscript

As I noticed later, if you put + in between and split at the end based on +, the amount of code you write will decrease.

answerC.py


s=input()
l=len(s)

ans=0
for i in range(2**(l-1)):
    ans_sub=0
    subst=s[0]
    for j in range(l-1):
        if (i>>j)&1:
            ans_sub+=int(subst)
            subst=s[j+1]
        else:
            subst+=s[j+1]
        if j==l-2:
            ans_sub+=int(subst)
    ans+=ans_sub
if l==1:
    print(int(s))
else:
    print(ans)

D problem

At first, I felt that I should check all the squares, but since it is O (HW), I can't make it in time. Here, if you pay attention to the number of squares to be filled, it is 10 ^ 5 or less, so if you focus on ** filling **, you can see that you can write a program. Here, when you fill a certain square, you can see that the square is included in the board of $ 3 \ times 3 $ centered on the adjacent squares. Therefore, the adjacent squares (of which the squares that make the board of $ 3 \ times 3 $) are saved, and the squares that are included in the board of $ 3 \ times 3 $ centered on that square appear. You can update it every time you come. Also, since it is impossible to save all the coordinates of the center in terms of spatial complexity **, I try to save only the coordinates that appear in the dictionary (all the squares that do not appear even once () (It can be the center) The number of squares $ (h-2) \ times (w-2) $ can be subtracted from the coordinates.)

answerD.py


h,w,n=map(int,input().split())
d=dict()
def change(a,b):
    global h,w,d
    if 1<=a<h-1 and 1<=b<w-1:
        if (a,b) in d:
            d[(a,b)]+=1
        else:
            d[(a,b)]=1
for i in range(n):
    a,b=map(int,input().split())
    a-=1
    b-=1
    for j in range(3):
        for k in range(3):
            change(a+j-1,b+k-1)
ans=[0]*10
ans[0]=(h-2)*(w-2)

for i in d:
    ans[d[i]]+=1
    ans[0]-=1
for i in range(10):
    print(ans[i])

Recommended Posts

AtCoder Beginner Contest 102 Review of past questions
AtCoder Beginner Contest 072 Review of past questions
AtCoder Beginner Contest 085 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 151 Review of past questions
AtCoder Beginner Contest 075 Review of past questions
AtCoder Beginner Contest 054 Review of past questions
AtCoder Beginner Contest 110 Review of past questions
AtCoder Beginner Contest 070 Review of past questions
AtCoder Beginner Contest 105 Review of past questions
AtCoder Beginner Contest 112 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 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 103 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 048 Review of past questions
AtCoder Beginner Contest 124 Review of past questions
AtCoder Beginner Contest 116 Review of past questions
AtCoder Beginner Contest 097 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 094 Review of past questions
AtCoder Beginner Contest 063 Review of past questions
AtCoder Beginner Contest 107 Review of past questions
AtCoder Beginner Contest 071 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