from Second Algorithm Practical Skills Test PAST2L
def solve(N, K, D, AS):
end = N - D * (K - 1)
start = 0
ret = []
for _i in range(K):
subseq = AS[start:end]
if not subseq:
return [-1]
v = min(subseq)
ret.append(v)
start = AS.index(v, start) + D
end += D
return ret
- 8 TLE
def solve(N, K, D, AS):
from heapq import heappush, heappop, heapify
end = N - D * (K - 1)
start = 0
ret = []
queue = [(AS[i], i) for i in range(start, end)]
heapify(queue)
for _i in range(K):
if start >= end:
return [-1]
while True:
v, i = heappop(queue)
if start <= i < end:
break
ret.append(v)
start = i + D
for i in range(end, min(end + D, N)):
heappush(queue, (AS[i], i))
end += D
return ret
This page is auto-translated from /nishio/PAST2L using DeepL. If you looks something interesting but the auto-translated English is not good enough to understand it, feel free to let me know at @nishio_en. I'm very happy to spread my thought to non-Japanese readers.