meiguiOvO @ 2025-01-10 17:27:30
def binary_search(nums, target):
left, right = 0, len(nums) - 1
result = -1
while left <= right:
mid = left + (right - left) // 2
if nums[mid] == target:
result = mid
right = mid - 1
elif nums[mid] < target:
left = mid + 1
else:
right = mid - 1
if result!= -1:
return result + 1
return result
n, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
d = 0
while d < m:
print(binary_search(a, b[d]))
d += 1
by pjh0625 @ 2025-01-10 17:37:50
@meiguiOvO
import sys
data = sys.stdin.read().split()
n = int(data[0])
m = int(data[1])
num = list(map(int, data[2:2 + n]))
queries = list(map(int, data[2 + n:2 + n + m]))
f = []
for q in queries:
l, r = 0, n
while l < r:
mid = (l + r) // 2
if num[mid] < q:
l = mid + 1
else:
r = mid
if l < n and num[l] == q:
f.append(l + 1)
else:
f.append(-1)
print(' '.join(map(str, f)))
虽然是我的代码。。。