You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
1.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int M = 1e6 + 10;
int m, n, K;//m表示学校数n表示学生
int a[M];
long long res;
int main() {
cin >> m >> n;
for (int i = 1; i <= m; i++) cin >> a[i];
sort(a + 1, a + m + 1);
/*根据n位学生的估分情况分别给每位学生推荐一所学校要求学校的预计分数线和学生的估分相差最小
(可高可低,毕竟是估分嘛),这个最小值为不满意度。求所有学生不满意度和的最小值。*/
for (int i = 1; i <= n; i++) {
cin >> k;
//通过二分,找到离此学生分数高的第一所大学,并计算出不满意度,累加
int l = 1, r = m;
while (l < r) {
int mid = l + r >> 1;
if (a[mid] >= k) r = mid;
else l = mid + 1;
}
//看不上的大学与考不上的大学,仔细口味下哪个的估分差最小。
if (l > 1) res += min(abs(a[l] - k), abs(a[l - 1] - k));
else res += abs(a[l] - k);
}
cout << res << endl;
return 0;
}