python有过的吗

P1678 烦恼的高考志愿

Echo5201 @ 2024-11-08 17:17:35

m,n=map(int,input().split())

xx = list(map(int,input().split()))#学校

xs = list(map(int,input().split()))#学生

xs.sort()#排序

xx.sort()

s=0

for i in xs:#学生

    ans=999999
    left=0
    right=len(xx)-1
    while(left<=right):
        mid = (left+right)//2
        if(xx[mid]==i):
          ans=0
          break
      elif(xx[mid]<i):
              ans = min(ans,abs(xx[mid]-i))#最小值
              left = mid+1
    elif(xx[mid]>i):
            ans = min(ans,abs(xx[mid]-i))
            right = mid-1 
s=s+ans

print(s)


by bsdsdb @ 2024-11-08 17:21:24

希丰展使md


by LionBlaze @ 2024-11-08 17:25:36

@Echo5201

  1. tlqtj,jbl
  2. 希丰展使 MD
  3. click

by Echo5201 @ 2024-11-08 17:25:56

@0x28202e202e29 大佬这是什么呢


by LionBlaze @ 2024-11-08 17:35:46

@Echo5201 望更富的现?使Markdown、KaTeX。


by Echo5201 @ 2024-11-08 18:34:13

@LionBlaze 欧克 我这就去 不好意思哈


by Echo5201 @ 2024-11-08 18:51:36

m,n=map(int,input().split())

学校

xx = list(map(int,input().split()))

学生

xs = list(map(int,input().split()))

排序

xs.sort()
xx.sort()

求和赋初值

s=0

学生遍历

for i in xs:

 ans=999999
 left=0
 right=len(xx)-1
 while(left<=right):
    mid = (left+right)//2
    if(xx[mid]==i):  
           ans=0
           break
    elif(xx[mid]<i):
           #最小值
           ans = min(ans,abs(xx[mid]-i))
           left = mid+1
    elif(xx[mid]>i):
           ans = min(ans,abs(xx[mid]-i))
           right = mid-1 

s=s+ans


by Echo5201 @ 2024-11-08 18:51:58

@LionBlaze 这样可以吗


by Echo5201 @ 2024-11-08 18:52:19

@0x28202e202e29 谢谢呢 这个可以吗


by LionBlaze @ 2024-11-08 18:53:34


by Echo5201 @ 2024-11-08 18:59:11

@LionBlaze 哈哈哈 前面不是说我代码发的不清楚吗 不是这个意思吗


| 下一页