ice_moon_soul @ 2023-10-28 14:24:26
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e6 + 10;
int a[N], b[N];
int n, m, ans=0;
int find(int x)
{
int l = 1, r = n;
while (l < r)
{
int mid = r + l >> 1;
if (a[mid] == x)
{
return mid;
}
if (a[mid] >= x)
r = mid;
else
l = mid+1;
}
return l;
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> a[i];
sort(a + 1, a + 1 + n);
for (int i = 1; i <= m; i++)
{
cin >> b[i];
if (b[i] > a[n]) ans += b[i] - a[n];
else if (b[i] < a[1]) ans += a[1] - b[i];
else
{
int e = find(b[i]);
ans += min(abs(a[e] - b[i]), abs(b[i] - a[e - 1]));
}
}
cout << ans;
return 0;
}
by ricky_lin @ 2023-10-28 14:44:07
十年 OI
一场空,不开 long long
见祖宗
还有建议平常的时候养成好习惯,在 e==1
的时候加一个特判(因为 e-1 == 0
),虽然说这道题不加也可以过。
by ice_moon_soul @ 2023-10-28 22:14:39
@ricky_lin 懂了,谢谢大佬 (又一次被long long 斩于马下)
by chen0227 @ 2023-11-08 13:46:14
@ricky_lin 我也是因为这个错了,谢谢点拨
by Franky100 @ 2023-11-11 20:06:57
谢谢大佬,我也是因为这个错的