YYPanda233 @ 2023-10-23 23:03:05
#include <bits/stdc++.h>
using namespace std;
int main()
{
int m = 0, n = 0, t = 0, sum = 0, b = 0, l = 0, r = 0, mid = 0;
cin >> m >> n;
int a[m], c[n] = {0};
for (int i = 0; i < m; i++)
{
cin >> a[i];
}
sort(a, a + m);
for (int i = 0; i < n; i++)
{
cin >> b;
t = 1000001;
l = 0;
r = m - 1;
while(l <= r)
{
mid = (l + r) / 2;
if(a[mid] == b)
{
t = 0;
break;
}
else if(a[mid] < b)
{
t = min(abs(a[mid] - b), t);
l = mid + 1;
}
else if(a[mid] > b)
{
t = min(abs(a[mid] - b), t);
r = mid - 1;
}
}
c[i] = t;
}
sort(c, c + n);
for (int i = 0; i < n; i++)
{
sum += c[i];
}
cout << sum;
return 0;
}
求大佬解答!!!
by CodeLuo @ 2023-10-25 19:52:51
sum 写 long long
by YYPanda233 @ 2023-10-28 14:05:16
@CodeLuo 谢谢大佬,AC了!