利姆露·三上悟 @ 2022-07-05 19:16:42
#include<bits/stdc++.h>
using namespace std;
int m,n,a[100000],b[100000],ans;
bool sb[1000000];
int jd(int x,int y)
{
if(x>y)return x-y;
else return y-x;
}
int main()
{
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+1+m);
for(int i=1;i<=n;i++)
{
int l=0,r=m;
while(l<r)
{
int mid=(r+l)/2;
if(a[mid]<=b[i])l=mid+1;
else r=mid;
}
if(b[i]<=a[1])ans+=a[1]-b[i];
else
ans+=min(jd(a[l-1],b[i]),jd(a[l],b[i]));
}
cout<<ans;
return 0;
}
WA了,但不知错在那里QWQ
by rexwzq @ 2022-07-15 13:05:36
int m,n,a[100001],b[100001],ans; bool sb[1000001];