AtCoder Beginner Contest 118 Review of past questions

Time required

スクリーンショット 2020-01-31 12.34.26.png

Impressions

D Problem misreading ... The D problem was interesting with a new form of DP, I like it quite a lot.

Problem A

Determine if b% a is 0

answerA.py


a,b=map(int,input().split())
print(a+b if b%a==0 else b-a)

B problem

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)

C problem

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

D problem

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

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 127 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 067 Review of past questions
AtCoder Beginner Contest 093 Review of past questions
AtCoder Beginner Contest 123 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 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 083 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 053 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