SBBSBSBSBSB @ 2024-01-27 15:52:48
#include <bits/stdc++.h>
#define N 5000+10
using namespace std;
int ans,l;
int main() {
int m,n,a[N],b[N];
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++) {
l=0;
int r=n+1;
while(l<r) {
int mid=(l+r)>>1;
if(a[mid]<=b[i])
l=mid+1;
else
r=mid;
}
if(b[i]<=a[1])
ans+=a[1]-b[i];
else
ans+=min(abs(a[l-1]-b[i]),abs(a[l]-b[i]));
}
cout<<ans;
return 0;
}
by luogu_user1089090 @ 2024-01-27 15:55:19
@SBBSBSBSBSB
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int a[300010];
int erfen(int L,int R,int k){
if(k<=a[L]){
return a[L];
}
if(k>=a[R]){
return a[R];
}
while(L<=R){
int mid=(L+R)/2;
if(k==a[mid]){
return a[mid];
}
else if(k<a[mid]){
R=mid-1;
}
else{
L=mid+1;
}
}
return ((a[L]-k)<(k-a[R]))?(a[L]):(a[R]);
}
int main(){
int n,k;
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
sort(a+1,a+n+1);
long long x=0;
for(int i=1;i<=k;i++){
int b;
scanf("%d",&b);
x+=abs(b-erfen(1,n,b));
}
printf("%lld",x);
return 0;
}
by SBBSBSBSBSB @ 2024-01-27 16:42:42
@zwdzjs 谢谢