求助

P1678 烦恼的高考志愿

13PaulGeorge @ 2023-11-18 17:26:49


m,n=map(int,input().split())
a=[int(i)for i in input().split()]
b=[int(i)for i in input().split()]
a.sort()
def fm(c):
    l=0 
    r=m
    while(l<r):
        mid=l+r>>1
        if a[mid]>=c:r=mid
        else: l=mid+1
    return l
def fp(c):
    l=0
    r=m-1
    while(l<r):
        mid=l+r+1>>1
        if a[mid]<=c:l=mid
        else:r=mid-1
        return l
ans=0
for i in b:
    k=min(fm(i),m-1);j=min(fp(i),m-1)
    ans+=min(abs(i-a[k]),abs(i-a[j]))
print(ans)

by masonxiong @ 2024-02-07 17:04:16

如果我没记错,python对缩进有非常严格的要求,if else 后面的语句必须换行+缩进


|