社区讨论

二分法 70分 实在改不动了

P1678烦恼的高考志愿参与者 2已保存回复 2

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
2 条
当前快照
1 份
快照标识符
@lo2od3an
此快照首次捕获于
2023/10/23 17:07
2 年前
此快照最后确认于
2023/10/23 17:07
2 年前
查看原帖
CPP
#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;
}

回复

2 条回复,欢迎继续交流。

正在加载回复...