Chen_08 @ 2023-11-26 14:30:30
#include<iostream>
#include<algorithm>
using namespace std;
const int N=100010;
long long n,m;
long long s[N];
long long g[N];
long long bu(long long x,long long y)
{
if(x>y) return x-y;
else return y-x;
}
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
cin>>m>>n;
for(long long i=1;i<=m;i++) cin>>s[i];
for(long long i=1;i<=n;i++) cin>>g[i];//cin
sort(s+1,s+1+m);
sort(g+1,g+n+1);
s[0]=-1000;
long long i=1,j=1;
long long res=0;
while(i<=m&&j<=n)
{
while(i<=m&&s[i]<g[j]) i++;
if(i<=m)
{
res+=min(bu(s[i],g[j]),bu(s[i-1],g[j]));
j++;
}
}
while(j<=n) {
res+=g[j]-s[m];
j++;
}
cout<<res;
return 0;
}
by Chen_08 @ 2023-11-26 22:37:20
找到问题了 s[0] 赋值小了 最大差值100000 应该把s[0]赋值-100000向下