yagyagyag @ 2020-04-04 19:39:15
#include<bits/stdc++.h>
using namespace std;
const int N=100005;
int n,m,a[N];
int find(int x)
{
int l=1,r=n,mid,ans;
while (l<=r){
mid=l+r>>1;
if (a[mid]<=x){
ans=mid;
l=mid+1;
}
else r=mid-1;
}
return ans;
}
int main()
{
cin>>n>>m;
for (int i=1;i<=n;i++) scanf("%d",a+i);
sort(a+1,a+n+1);
int ans=0;
for (int i=1;i<=m;i++){
int x;
scanf("%d",&x);
int pos=find(x),minn=2e9;
for (int j=max(1,pos-2);j<=min(n,pos+2);j++)
minn=min(minn,abs(a[j]-x));
ans+=minn;
}
cout<<ans<<endl;
return 0;
}
by 翼德天尊 @ 2020-04-24 22:03:57
@metaphysis 您的回复可以证明您是个厉害的编程者,在下表示深感佩服,已关注您。