yhm_22344 @ 2024-01-21 12:40:37
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int N = 1e5 + 10;
int arr[N], rell[N];
int n, m;
int main()
{
//m为学校数,n为学生数
cin >> m >> n;
//arr[i]表示学校预计录取分数线
//rell[i]表示学生估分
for (int i = 0; i < m; i++) cin >> arr[i];
sort(arr, arr + m);
long long res = 0;
for (int i= 0; i < n; i++)
{
cin >> rell[i];
int l = 0, r = m - 1;
while (l < r)
{
int mid = l + r >> 1;
if (arr[mid] >= rell[i]) r = mid;
else l = mid + 1;
}
res += min(abs(arr[l] - rell[i]), min(abs(arr[l - 1] - rell[i]), abs(arr[l+1] - rell[i])));
}
cout << res << endl;
return 0;
}
by shuling @ 2024-01-23 17:28:00
就是比录取分最高的还要高或者比最低的还要低 if(rell[i]<=arr[0]){ res+=abs(arr[0]-rell[i]);continue;} if(rell[i]>=arr[m-1]){ res+=abs(arr[m-1]-rell[i]);continue;}