AcWing 800. 数组元素的目标和
原题链接
简单
作者:
将情怀讲泛滥的恶果
,
2021-03-29 10:20:54
,
所有人可见
,
阅读 165
双指针法
if __name__=="__main__":
n,m,k = [int(x) for x in input().split()]
a=[int(x) for x in input().split()]
b=[int(x) for x in input().split()]
i=0
j=m-1
while a[i]+b[j]!=k and i<n and j>=0:
if a[i]+b[j]>k:
j-=1
elif a[i]+b[j]<k:
i+=1
else:
break
print(str(i)+" "+str(j))
二分法
def binary_search(nums,target):
l=0
r=len(nums)-1
while l<r:
mid = (l+r)>>1
if check(nums[mid],target):
r=mid
else:
l=mid+1
if target==nums[l]:
return l
else:
return -1
def check(num,target):
if num>=target:
return True
else:
return False
if __name__=="__main__":
n,m,x = [int(x) for x in input().split()]
a=[int(x) for x in input().split()]
b=[int(x) for x in input().split()]
for i in range(0,n):
target = x - a[i]
index=binary_search(b,target)
if index > -1:
print(str(i) + " "+str(index))