shining_array @ 2022-10-03 09:00:16
#include<bits/stdc++.h>
using namespace std;
int a[100005],b[100005],l,r,tot=0,n,m;
int binary(int x)
{
l=0,r=n-1;
while(l<=r)
{
int mid=(l+r)/2;
if(x<a[mid]&&x>a[mid-1]) return min(abs(a[mid-1]-x),abs(a[mid]-x));
else if(x>a[mid]&&x>a[mid-1]) l=mid+1;
else if(x<a[mid]&&x<a[mid+1]) r=mid-1;
}
}
int main()
{
cin>>n>>m;
for(int i=0;i<n;i++) cin>>a[i];
for(int i=0;i<m;i++) cin>>b[i];
sort(a,a+n);
for(int i=0;i<m;i++) tot+=binary(b[i]);
cout<<tot;
return 0;
}
by BIG_CUTE_BUG @ 2022-10-03 09:08:36
二分?
不是先找出l
和r
再return min(abs(a[mid-1]-x),abs(a[mid]-x))
吗?