WA#11

P1678 烦恼的高考志愿

tyh0929 @ 2024-09-25 20:06:55

#include<bits/stdc++.h>
using namespace std;
long long n,m,a[100001],b;
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    sort(a+1,a+n+1);
    a[0]=-3000000;
    a[n+1]=3000000;
    long long sum=0;
    for(int i=1;i<=m;i++){
        scanf("%d",&b);
        int l=0,r=n+1;
        while(l<r){
            int mid=l+(r-l)/2;
            if(a[mid]<=b){
                l=mid+1;
            }else{
                r=mid;
            }
        }
        sum+=min(abs(a[l]-b),abs(a[l-1]-b));
    }
    printf("%lld",sum);
    return 0;
}

|