[PYTHON] AtCoder Beginner Contest 088 Review of past questions

Time required

スクリーンショット 2020-04-01 16.39.23.png

Impressions

This is a past question I solved before, but I made the same mistake again with the D problem. I would like to mention the content of the mistake in the D problem.

Problem A

Since we have an infinite number of 500 yen currencies, consider whether the remainder of dividing n by 500 is less than a.

answerA1.py


n=int(input())
a=int(input())
print("Yes" if n%500<=a else "No")

B problem

The strategy for maximizing their scores is to have Alice take the 0-indexed and even-numbered ones and Bob take the odd-numbered ones in descending order.

answerB.py


n=int(input())
a=sorted(list(map(int,input().split())),reverse=True)
ans=0
for i in range(n):
    if i%2==0:
        ans+=a[i]
    else:
        ans-=a[i]
print(ans)

C problem

For the i-th row, it is $ a_i + b_1, a_i + b_2, a_i + b_3 $, and for each row (value in the second column-1 value in the first column) and (value in the third column-2nd column). ) Are the conditions necessary for Takahashi's information to be correct, and it becomes clear from experiments that it is sufficient to set the value of a appropriately when this condition is satisfied. (Although the explanation is appropriate, it is clear when you actually calculate (value in the second column-value in the first column) and (value in the third column-value in the second column) respectively.)

answerC.py


c=[list(map(int,input().split())) for i in range(3)]
x=c[0][2]-c[0][1]
y=c[0][1]-c[0][0]
for i in range(1,3):
    if c[i][1]-c[i][0]!=y:
        print("No")
        break
    if c[i][2]-c[i][1]!=x:
        print("No")
        break
else:
    print("Yes")

D problem

I remembered how to solve this problem because it was impressive to solve it before, but I made many mistakes at the end. Sunuke wants to improve his score as much as possible, so he needs to change as many squares as possible to black. However, if you change too many squares, you will not be able to reach the goal. In other words, all you have to do is fill in everything except the squares that Kenusu follows and goes to the goal, and ** to maximize, you can fill in everything except the shortest path **. Therefore, this problem can be regarded as a simple shortest path problem. I wrote the code with dfs. However, in the case of Python, the upper limit of the depth of recursion is set by each environment, so `sys.setrecursionlimit (x)` sets the limit of recursion to x **. .. However, if this ** x is too large, it will be TLE, so it should be as small as possible **. In this problem, H is 50 and W is 50, so I thought it would be good if it was 2500 or more, but since it became RE, there seems to be a pattern where the number of recursion is more than 2500 (I can not think of it ...) .. I tried to set the number of recursion to the limit, but it didn't work because it was the intention of Writer. At the end, I made it work by speeding up with ** PyPy . Also, when finding the shortest distance, breadth-first search is more effective, and the Writer solution is breadth-first search ( Depth-first search would search too deeply **).

answerD.py


import sys
h,w=map(int,input().split())
sys.setrecursionlimit(h*w+10)
s=[list(input()) for i in range(h)]
inf=1000000000000000
d=[[inf]*w for i in range(h)]


def dfs(i,j):
    global h,w,s
    nex=[(0,-1),(0,1),(-1,0),(1,0)]
    for k in range(4):
        l,m=i+nex[k][0],j+nex[k][1]
        if 0<=i+nex[k][0]<h and 0<=j+nex[k][1]<w:
            if s[l][m]=="." and d[l][m]>d[i][j]+1:
                d[l][m]=d[i][j]+1
                dfs(l,m)
d[0][0]=0
dfs(0,0)
ans=0
for i in range(h):
    for j in range(w):
        if s[i][j]==".":
            ans+=1
if d[h-1][w-1]==inf:
    print(-1)
else:
    print(ans-d[h-1][w-1]-1)

Recommended Posts

AtCoder Beginner Contest 102 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 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 117 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 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 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 047 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 090 Review of past questions
AtCoder Beginner Contest 103 Review of past questions
AtCoder Beginner Contest 061 Review of past questions
AtCoder Beginner Contest 083 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 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 071 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