Rightit @ 2024-08-07 16:28:58
#include<bits/stdc++.h>
using namespace std;
int m,n,mid,ans=0;
int a[1000005],b[1000005]; //a学校线,b学生线
int main()
{
cin>>n>>m;
for (int i=1;i<=n;i++)
{
cin>>a[i];
}
for (int i=1;i<=m;i++)
{
cin>>b[i];
}
sort(a+1,a+n+1);//小到大
for (int i=1;i<=m;i++)
{
int l=0,r=n+1;//左右区间
while(l<r)
{
mid=(l+r)/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(abs(a[l-1]-b[i]),abs(a[l]-b[i]));//求差
}
cout<<ans;
return 0;
}
by Rightit @ 2024-08-09 14:45:07
@Endless_summer 好的,谢谢
by Rightit @ 2024-08-09 14:45:31
此帖已结
by wasq @ 2024-08-17 16:03:02
@Rightit 开long long就OK了