Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.1 kB
3
Indexable
Never
#bài 1
def search(x,ds, l, r):
  if l>=r:
    try:
      if abs(ds[l]-x) <= abs(ds[r]-x):
        return l
      else:
        return r
    except:
      if r < 0:
        return 0
      if l>= len(ds):
        return len(ds-1)    

  mid= (l+r)//2
  
  if x < ds[mid]:
    return search(x, ds, l, mid-1)
  elif x> ds[mid]:
    return search(x, ds, mid+1, r)  
  else:
    return mid  

n = int(input())
ds = list(map(int,input().split()))
k, x = map(int,input().split())

loc = search(x,ds, 0, len(ds)-1)

output = [ds[loc]]
off_r, off_l= 1, 1

while len(output)< k:

  if loc + off_r >= len(ds):
    for i in range(k-len(output)):
      output.append(ds[loc-off_l])
      off_l+=1
  elif loc - off_l < 0:
    for i in range(k-len(output)):
      output.append(ds[loc+off_r])
      off_r+=1 
  else:
    if abs(ds[loc- off_l]-x) <= abs(ds[loc + off_r] - x):
      output.append(ds[loc-off_l])
      off_l+=1  
    else:
      output.append(ds[loc+off_r])
      off_r+=1   

output.sort()
for num in output:
  print(num,end = " ")