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