二分法 70分 实在改不动了
查看原帖
二分法 70分 实在改不动了
914039
yukq11楼主2023/4/30 16:48
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=1e6+100;
int n,m;
int stu[maxn];
int sch[maxn];
ll ans=0; 

int main(){
	cin>>m>>n;//n:学生 m:学校
	for(int i = 0; i < m; i++) cin>>sch[i];
	for(int i = 0; i < n; i++) cin>>stu[i];
	sort(sch,sch+m);
	int pos;
	for(int i = 0; i < n; i++){
		pos=lower_bound(sch,sch+m,stu[i])-sch;
		//cout<<pos<<' '<<stu[i]<<' ';
		if(pos==0) ans+=abs(stu[i]-sch[0]);
		else if(pos==n) ans+=abs(stu[i]-sch[n-1]);
		else{
			ans+=min(abs(stu[i]-sch[pos]),abs(stu[i]-sch[pos-1]));
		}
		//cout<<ans<<endl;
	}
	cout<<ans;
	return 0;
}
2023/4/30 16:48
加载中...