#include<stdlib.h>
#include<stdio.h>
#include<math.h>
int Ffirst(int* arr, int left, int right, int t)
{
int res = 100001;
while (left <= right)
{
int mid = (left + right) / 2;
if (arr[mid] >= t)
{
right = mid - 1;
res = mid;
}
else
left = mid + 1;
}
return res;
}
int cmp(const void* p1, const void* p2)
{
return *(int*)p1 - *(int*)p2;
}
int main()
{
int n, m;
scanf("%d%d", &n, &m);
int sch[100000];
for (int i = 0; i < n; i++)
scanf("%d", &sch[i]);
int stu[100000];
for (int i = 0; i < m; i++)
scanf("%d", &stu[i]);
qsort(sch, n, sizeof(sch[0]), cmp);
long long int count = 0;
for (int i = 0; i < m; i++)
{
int t = stu[i];
int next = Ffirst(sch, 0, n - 1, t);
int min = 0;
if (next == 0)
{
min = abs(sch[next]-t);
}
else if (next == 100001)
{
min = abs(sch[n - 1] - t);
}
else
{
int a1 = abs(sch[next - 1] - t);
int a2 = abs(sch[next] - t);
if (a1 > a2)
min = a2;
else
min = a1;
}
count += min;
}
printf("%lld", count);
return 0;
}
烦恼的高考志愿
于 2024-12-03 22:24:37 首次发布