Files
python/TangDou/LuoGuBook/P1678.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

32 lines
1.1 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
}