liyunhe @ 2024-05-18 12:38:52
#include<bits/stdc++.h>
using namespace std;
long long s[1000100],a[1000100];
long long n,m,i,j=1,cnt=0;
int main()
{
cin>>m>>n;
for(i=1; i<=m; i++)cin>>s[i];
for(i=1; i<=n; i++)cin>>a[i];
sort(s+1,s+m+1),sort(a+1,a+n+1);
for(i=1; i<=n; i++)
{
while(s[j]<a[i])j++;
cnt+=min((j==1?10000000:a[i]-s[j-1]),s[j]-a[i]);
}
cout<<cnt;
return 0;
}
by HEROBRINEH @ 2024-05-18 12:51:16
AC
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
#define re register
const int maxn=100001;
long long a[maxn],b[maxn];
int main(){
re long long m,n,k=1,sum=0;
cin>>m>>n;
for(re int i=1;i<=m;i++)cin>>b[i];
for(re int i=1;i<=n;i++)cin>>a[i];
sort(a+1,a+n+1);
sort(b+1,b+m+1);
for(re int i=1;i<=n;i++){
re long long p=abs(a[i]-b[k]);
while(abs(a[i]-b[k+1])<=p){
k++;
p=abs(a[i]-b[k]);
}
sum+=p;
}
cout<<sum;
return 0;
}
by liyunhe @ 2024-05-25 11:57:33
@HEROBRINEH 谢谢 问题已解决