peach2 @ 2022-11-26 19:41:24
#include<bits/stdc++.h>
using namespace std;
int m,n;
int a[100005],b[100005];
int ans=0;
int cal(int x){
int l=1,r=m;
int mini=100;
while(l<=r){
int mid=(l+r)/2;
if(a[mid]==x) return 0;
else if(a[mid]-x > 0){
mini=min(abs(a[mid]-x),mini);
r=mid-1;
}
else if(a[mid]-x < 0){
mini=min(abs(a[mid]-x),mini);
l=mid+1;
}
}
return mini;
}
int main(){
cin>>m>>n;
for(int i=1;i<=m;++i) cin>>a[i];//分数线
for(int i=1;i<=n;++i) cin>>b[i];//估分
sort(a+1,a+m+1);
for(int i=1;i<=n;++i) {
ans+=cal(b[i]);
}
cout<<ans;
return 0;
}
by zzf12345666 @ 2023-02-06 00:16:36
是不是你mini太小了,mini得弄大点,long long赋值大一点试试