Advertisement
mfgnik

Untitled

Oct 6th, 2020
1,041
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.71 KB | None | 0 0
  1. n = int(input())
  2. settle = list(map(int, input().split()))
  3. m = int(input())
  4. bomb = list(map(int, input().split()))
  5. for i in range(len(bomb)):
  6.     bomb[i] = (i + 1, bomb[i])
  7. bomb.sort(key=lambda x: x[1])
  8.  
  9.  
  10. def find_value(x):
  11.     if x < bomb[0][1]:
  12.         return bomb[0][0]
  13.     if x > bomb[-1][1]:
  14.         return bomb[-1][0]
  15.     left = 0
  16.     right = m - 1
  17.     while right - left > 1:
  18.         medium = (right + left) // 2
  19.         if bomb[medium][1] < x:
  20.             left = medium
  21.         else:
  22.             right = medium
  23.     if x - bomb[left][1] < bomb[right][1] - x:
  24.         return bomb[left][0]
  25.     else:
  26.         return bomb[right][0]
  27.  
  28.  
  29. for i in range(n):
  30.     print(find_value(settle[i]), end=' ')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement