90!!求助!!

P1678 烦恼的高考志愿

dgz61 @ 2024-12-31 14:22:35

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll m,n,a[100000],ans=0,x,d;
int main(){
    cin>>m>>n;
    for(ll i=1;i<=m;i++) cin>>a[i];
    sort(a+1,a+m+1);
    for(ll i=1;i<=n;i++){
        cin>>x;
        d=lower_bound(a+1,a+m+1,x)-a;
        if(d==m+1) ans+=x-a[m];
        else if(d==1) ans+=a[1]-x;
        else ans+=min(abs(a[d]-x),abs(x-a[d-1]));
    }
    cout<<ans;
    return 0;
}

求助!


|