Submerge69 @ 2024-02-27 17:05:02
#include<bits/stdc++.h>
using namespace std;
int n,m,a[100010],b[100010];
long long num;
int f;
void check(int temp){
int l=1,r=m,mid;
f=abs(a[1]-temp);
while(l<=r){
mid=l+(r-l)/2;
if(a[mid]>temp) r=mid-1;
else l=mid+1;
f=abs(temp-a[mid]);
}
if(mid+1<=m) f=min(f,abs(temp-a[mid+1]));
if(mid-1>=1) f=min(f,abs(temp-a[mid-1]));
return ;
}
int main(){
scanf("%d %d",&m,&n);
for(int i=1;i<=m;i++) scanf("%d",&a[i]);//录取线
for(int i=1;i<=n;i++) scanf("%d",&b[i]);//成绩
sort(a+1,a+m+1);
for(int i=1;i<=n;i++){
f=2000000;
check(b[i]);
num+=f;
}
printf("%d",num);
return 0;
}
by zhenghbkh @ 2024-03-10 10:21:44
@zhang 时间够,但是他感觉不大会用printf