1 条题解
-
1
#include <bits/stdc++.h> using namespace std;
const int maxn = 1e5 + 100; long long n, m, a[maxn], b[maxn];
int main() { cin >> m >> n; for (int i = 1; i <= m; i++) { cin >> a[i]; } for (int i = 1; i <= n; i++) { cin >> b[i]; } sort (a + 1, a + 1 + m); long long sum = 0; for (int i = 1; i <= n; i++) { int pos = lower_bound(a + 1, a + 1 + m, b[i]) - a; long long mn = 2e9; if (pos <= m) { mn = min(mn, abs(b[i] - a[pos])); } if (pos > 1) { mn = min(mn, abs(b[i] - a[pos - 1])); } sum += mn; } cout << sum << endl; return 0; }
- 1
信息
- ID
- 4
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 7
- 标签
- 递交数
- 366
- 已通过
- 73
- 上传者