xzyNOIP @ 2023-08-03 18:02:49
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll m,n;
ll sch[100010],stu[100010];
ll sum;
int main()
{
cin>>m>>n;
for(ll i=0;i<m;i++)
{
cin>>sch[i];
}
for(ll i=0;i<n;i++)
{
cin>>stu[i];
}
sort(sch,sch+m);
for(ll i=0;i<n;i++)
{
ll index=lower_bound(sch,sch+m,stu[i])-sch;
if(index==0)
{
sum+=sch[0]-stu[i];
continue;
}
sum+=min(sch[index]-stu[i],stu[i]-sch[index-1]);
}
cout<<sum;
return 0;
}
by lijuncheng_1207 @ 2023-08-03 18:48:17
改好了!
#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;
}
by xzyNOIP @ 2023-08-03 19:18:27
@Lijuncheng1207 谢谢!