D Problem misreading ... The D problem was interesting with a new form of DP, I like it quite a lot.
Determine if b% a is 0
answerA.py
a,b=map(int,input().split())
print(a+b if b%a==0 else b-a)
I took too much time to implement it ... You can judge whether everyone likes each of the m types.
answerB.py
n,m=map(int,input().split())
x=[[int(j) for j in input().split()][1:] for i in range(n)]
cnt=0
for i in range(1,m+1):
for j in range(n):
if i not in x[j]:
break
else:
cnt+=1
print(cnt)
After the monster with the lowest health among the given monsters cuts off the health of the other monsters, the monster with the lowest health among the remaining monsters cuts the health of the other monsters. By repeating this operation, the last remaining monster is only one, so the solution required for this monster's physical strength. When I tried it with a sample etc., it worked, and I thought that it would be impossible to minimize it, so I wrote the following code. (Answer says that the greatest common divisor of the physical strength of all monsters is the solution that I am surely doing. Even if you consider the operation, you can see that it is the greatest common divisor.)
answerC.py
n=int(input())
a=list(map(int,input().split()))
while len(a)>1:
l=len(a)
a.sort()
b=[a[0] if i==0 else a[i]%a[0] for i in range(l)]
a=[b[i] for i in range(l) if b[i]!=0]
print(a[0])
First, the first code is a typical (?) Wrong code. I was thinking of it as ** N or less ** instead of just N **. Actually, if it is ** N or less **, you should think greedily, but if it is ** just N **, you need to think about the combination **, so DP cannot be used. Is it? I came up with the idea.
If you can come up with a DP, it's not too difficult. If you think of ** dp [i] as the maximum number that can be represented when using exactly i ** (note that a is sorted in descending order by numeric value), then a [i] [0] The i-th largest number, a [i] [1], is the number of matchsticks needed to represent that number, `dp [j + a [i] [1]]] = max (dp [j +] a [i] [1]], dp [j] * 10 + a [i] [0]) You can find dp [n] by updating
`. Also, once a certain set of numbers is decided, the number of matches is uniquely determined ** It is best to enter the largest numbers in order from the top digit **, so sort a in descending order and take the DP in order from the largest number. You can achieve this by doing (eg 9 → 3 → 3 → 2 in that order, $ ((((0 \ times 10 + 9) \ times 10 + 3) \ times 10 + 3) \ times 10 + 2) = 9332 $, and it certainly holds. ** I feel that this is the first DP of the type that gives different results in the order of the elements. **).
answerD_WA.py
n,m=map(int,input().split())
x=[2,5,5,4,5,6,3,7,6]
a=[]
for i in map(int,input().split()):
a.append([i,x[i-1]])
mi=a[0]
for i in range(1,m):
if mi[1]>a[i][1] or (mi[1]==a[i][1] and a[i][0]>mi[0]):
mi=a[i]
a=[a[i] for i in range(m) if a[i][0]>=mi[0]]
m=len(a)
b=sorted(a,key=lambda x:x[1])
c=sorted(a,reverse=True)#Finally mi
d1=n//mi[1]#Number of digits
ans=[mi[0]]*d1
d2=n%mi[1]#How much can be used later
now=0#What are you trying to change in ans
for i in range(m-1):
y=d2//(c[i][1]-mi[1])
if y!=0:
for j in range(now,now+y):
ans[j]=c[i][0]
now+=y
d2-=(c[i][1]-mi[1])*y
if d2==0:
break
cnt=0
for i in range(d1):
cnt=cnt*10+ans[i]
print(cnt)
answerD.py
n,m=map(int,input().split())
x=[2,5,5,4,5,6,3,7,6]
a=[]
for i in map(int,input().split()):
a.append([i,x[i-1]])
a.sort(reverse=True)
dp=[0]*(n+1)
for i in range(m):
for j in range(n+1):
if j==0 or dp[j]!=0:
if j+a[i][1]<=n:
dp[j+a[i][1]]=max(dp[j+a[i][1]],dp[j]*10+a[i][0])
print(dp[n])
Recommended Posts