lucy2012 @ 2024-03-20 18:12:34
#include<bits/stdc++.h>
using namespace std;
int m,n,a[10000010],b[10000010],l=0,r=10000000,mid,sum=0;
int find(int num){
int l=1,r=10000000;
while(l<r){
mid=l+r>>1;
if(num>=mid)
l=mid;
else
r=mid;
}
if(abs(a[l]-num)>abs(a[r]-num))
return abs(a[r]-num);
else
return abs(a[l]-num);
}
int main(){
cin>>m>>n;
for(int i=1;i<=m;i++)
cin>>a[i];
sort(a+1,a+1+m);
for(int i=1;i<=n;i++){
cin>>b[i];
sum+=find(b[i]);
}
cout<<sum;
return 0;
}
by fight_for_humanity @ 2024-03-20 18:37:42
跑到
by lucy2012 @ 2024-03-20 18:47:11
@fight_for_humanity 该怎么改?QAQ
by lucy2012 @ 2024-03-20 18:55:53
这样不超时但是错的
#include<bits/stdc++.h>
using namespace std;
int m,n,a[10000010],b[10000010],l=0,r=10000000,mid,sum=0;
int find(int num){
int l=1,r=10000000;
while(l<r){
mid=l+r>>1;
if(num>=a[mid])
l=mid+1;
else
r=mid-1;
}
if(abs(a[l]-num)>abs(a[r]-num))
return abs(a[r]-num);
else
return abs(a[l]-num);
}
int main(){
cin>>m>>n;
for(int i=1;i<=m;i++)
cin>>a[i];
sort(a+1,a+1+m);
for(int i=1;i<=n;i++){
cin>>b[i];
sum+=find(b[i]);
}
cout<<sum;
return 0;
}
by fight_for_humanity @ 2024-03-20 22:12:08
二分得到的结果