LuminaryWay @ 2023-08-03 18:49:40
#include<bits/stdc++.h>
using namespace std;
int s[100005],t[100005],sum;
int main(){
int n,m;
cin >> m >> n;
for(int i = 1;i <= m;i++){
cin >> s[i];
}
for(int i = 1;i <= n;i++){
cin >> t[i];
}
sort(s + 1,s + m + 1);
for(int i = 1;i <= n;i++){
if(t[i] >= s[m]) sum += t[i] - s[m];
else if(t[i] <= s[1]) sum += s[1] - t[i];
else{
int num = upper_bound(s + 1,s + m + 1,t[i]) - s;
if(s[num] > t[i]) sum += s[num] - t[i];
}
}
cout << sum << endl;
return 0;
}
by lijuncheng_1207 @ 2023-08-03 18:52:47
#include <bits/stdc++.h>
using namespace std;
const int N=1e8;
long long n,a[N],x,m,cnt;
int main(){
cin>>n>>m;;
for(int i=1;i<=n;i++){
cin>>a[i];
}
sort(a+1,a+n+1);
while(m--){
cin>>x;
int l=1,r=n;
while(l+1<r){
int mid=(l+r)>>1;
if(a[mid]>=x)
r=mid;
else
l=mid;
}
cnt+=abs(a[l]-x)<=abs(a[r]-x)?abs(a[l]-x):abs(a[r]-x);
}
cout<<cnt;
return 0;
}
nice