[PYTHON] ABC123D Review of past questions

I reviewed ABC123 in Separate article, but I can post the AC solution. I didn't, so I would like to post the AC solution and explain it in this article. Kenchon's article and Explanation I am referring to it.

スクリーンショット 2020-01-19 14.39.57.png

AC solution 1 (Writer solution solution 3)

It is a method using Priority_queue. Insert the total size into the Priority_queue in order as the priority. ** There is no combination of cakes that are not in Priority_queue and have a higher priority than the combination of cakes that are in Priority_queue (✳︎) ** If you can perform the insert operation while guaranteeing), you can output k of the Priority_queue with the highest priority in order. However, it is clear that if you try all the cake combinations, you will not be able to make it in time due to restrictions. Specifically, a, b, and c are sorted in ascending order after adding- (substantial descending sort, processing for extracting in descending order of priority with heapq), and a [0) with the highest priority among them. ], B [0], c [0] combination is inserted in Priority_queue. Consider the next most likely combination in this situation. Then, one of a [1], b [0], c [0], a [0], b [1], c [0], a [0], b [0], c [1] You can see that it is a combination of. Generalizing this, the next highest priority combination after ** a [j], a [k], a [l] is a [j + 1], a [k], a [l], a [ It can be one of j], a [k + 1], a [l], a [j], a [k], a [l + 1] . Therefore, the highest priority element of Priority_queue is extracted, and for that element a [j + 1], a [k], a [l], a [j], a [k + 1], a [l], By inserting all three ways of a [j], a [k], and a [l + 1] into Priority_queue, it is possible to insert while satisfying (✳︎). Also, a [j + 1], a [k], a [l], a [j], a [k + 1], a [l], a [j], a [k], a [l + Since there is a possibility that it has already been inserted when inserting the three ways of 1], I used set to check whether it was inserted. In addition, it should be noted that the output needs to be marked with-because heapq is marked with-to retrieve in descending order of priority. ( I learned for the first time that inserting with a tuple makes the first element a priority criterion. **)

answerD.py


import heapq
x,y,z,k=map(int,input().split())
#Since heapq can only be arranged in ascending order,-If you add, it will be in descending order
a=sorted([-int(i) for i in input().split()])
b=sorted([-int(i) for i in input().split()])
c=sorted([-int(i) for i in input().split()])
check=set()
ans=[]
heapq.heappush(ans,(a[0]+b[0]+c[0],0,0,0))
check.add((0,0,0))
for i in range(k):
    h=heapq.heappop(ans)
    print(-h[0])
    if h[1]+1<x:
        if (h[1]+1,h[2],h[3]) not in check:
            heapq.heappush(ans,(a[h[1]+1]+b[h[2]]+c[h[3]],h[1]+1,h[2],h[3]))
            check.add((h[1]+1,h[2],h[3]))
    if h[2]+1<y:
        if (h[1],h[2]+1,h[3]) not in check:
            heapq.heappush(ans,(a[h[1]]+b[h[2]+1]+c[h[3]],h[1],h[2]+1,h[3]))
            check.add((h[1],h[2]+1,h[3]))
    if h[3]+1<z:
        if (h[1],h[2],h[3]+1) not in check:
            heapq.heappush(ans,(a[h[1]]+b[h[2]]+c[h[3]+1],h[1],h[2],h[3]+1))
            check.add((h[1],h[2],h[3]+1))

AC solution 2 (Writer solution solution 1?)

Is the previous solution quite technical? It was a simple solution (** I think it's basic considering how to handle Priority_queue. **), but I think this method is extremely natural. In the first place, this problem has the characteristic that the amount of calculation becomes large because A, B, and C can be selected in any order. Therefore, it is thought that the amount of calculation can be saved by ** first narrowing down the selection method between the two and then thinking about the remaining one ** (I used to do this method, but it is useless. I did too much. I felt that ** the problem should be simplified **.). If you think about x * y first, it will be $ 10 ^ 6 $ at the maximum, but since k is 3000 or less, you can see that you do not need to think about $ 10 ^ 6 . Therefore, we know that we should consider the top kth of the combination of a and b. After that, by considering c, it can be reduced to O ( x \ times y + k \ times z $). However, as it is, it is necessary to calculate the maximum amount of calculation of $ 3 \ times 10 ^ 6 $, which is the last-minute calculation in Python. Therefore, it is necessary to work hard to increase the speed by a constant factor here. The speedups I've done are (1) use sort without using sorted (the difference between destructive and non-destructive, sorted is a little slower), and (2) even in the case of double loops, use the inclusion notation. Especially the latter is much faster. ** If you think the loop is slow, use the comprehension **.

Postscript

When I checked it while writing, sorted was faster than sorted. I don't know why. I would appreciate it if you could tell me. In addition, put everything in the main function and treat it as a local variable. There are speedups such as running with PyPy. Especially PyPy is insanely fast. The same code is about 3-5 times faster than Python.

answerD.py


x,y,z,k=map(int,input().split())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
c=[int(i) for i in input().split()]
a.sort()
b.sort()
c.sort()
ab=[a[i]+b[j] for j in range(y) for i in range(x)]
ab.sort(reverse=True)
ab=ab[:k]
l=len(ab)
abc=[ab[i]+c[j] for j in range(z) for i in range(l)]
abc.sort(reverse=True)
for i in range(k):
    print(abc[i])

answerD.py


x,y,z,k=map(int,input().split())
a=sorted([int(i) for i in input().split()])
b=sorted([int(i) for i in input().split()])
c=sorted([int(i) for i in input().split()])
ab=sorted([a[i]+b[j] for j in range(y) for i in range(x)],reverse=True)
ab=ab[:k]
l=len(ab)
abc=sorted([ab[i]+c[j] for j in range(z) for i in range(l)],reverse=True)
for i in range(k):
    print(abc[i])

Recommended Posts

ABC123D Review of past questions
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 117 Review of past questions
AtCoder Beginner Contest 070 Review of past questions
AtCoder Beginner Contest 105 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 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 047 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 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 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