https://www.luogu.com.cn/problem/P1678
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include <map>
typedef long long LL;
using namespace std;
int read(){
int x = 0,f = 1;
char c = getchar();
while (c<'0'||c>'9') {
if (c=='-') {
f = -1;
}
c = getchar();
}
while (c>='0'&&c<='9') {
x = x*10+c-'0';
c = getchar();
}
return x*f;
}
using namespace std;
int m,n,a[100002],score,pos,i;
long long ans;
int main()
{
m = read();
n = read();
for(i=1;i<=m;i++){
a[i] = read();
}
sort(a+1,a+m+1);//先排序一下
for(i=1;i<=n;i++)
{
score = read();
pos = lower_bound(a+1,a+m+1,score)-a;//返回查询到的位置
if(pos == m+1){
ans+=score-a[m];//特判比所有数都大的情况
} else {
if(pos==1){//特判比所有数都小的情况
ans+=a[1]-score;
}else{
ans += min(abs(a[pos]-score),abs(score-a[pos-1]));
}
}
}
cout<<ans;
return 0;
}
/*
4 3
513 598 567 689
500 600 550
============
32
*/
网友评论