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