k-th smallest number

 avatar
user_0443324754
python
3 years ago
507 B
61
Indexable
from heapq import *
n, k = tuple(map(int,input().split()))
A = list(map(int,input().split())) 
result = ""
h = []
for i in range(k + 1):          #generate the A[1,k];
    heappush(h, -A[i])
result= result+str(-h[0])+" "
for j in range(k,n):            #compare the A[k+1] with h[0](smallest) and ,until k+1 = n;
    if A[j] < -h[0]:            #if A[k+1] smaller, change the order.(replace A[k+1] with h[0]) 
        -heapreplace(h,-A[j])
    result= result+str(-h[0])+" "
print(result.strip())
Editor is loading...