80723 - 同测活动第4场完善程序1
统计题目(材料题)
(1)(推荐学校)给定长度为n的数组 a和长度为m的数组 b,对 b 中每个元素,在 a 中找与它最接近的数,计算差的绝对值并累加,输出总和。
01 #include <iostream>
02 #include <cmath>
03 #include <algorithm>
04 using namespace std;
05 int a[100100], b[100100];
06
07 int main() {
08 int n, m;
09 cin >> n >> m;
10 for (int i = 1; i <= n; i++)
11 cin >> a[i];
12 for (int i = 1; i <= m; i++)
13 cin >> b[i];
14 ①;
15 int ans = 0;
16 for (int i = 1; i <= m; i++) {
17 int l = 0, r = n + 1;
18 while (l < r) {
19 ②;
20 if (③)
21 l = mid + 1;
22 else
23 r = mid;
24 }
25 if (b[i] <= a[1])
26 ④ ;
27 else
28 ⑤ ;
29 }
30 cout << ans;
31 return 0;
32 }

关注我们