From 7db766b4e76e14416728451abee98e89c5bae79b Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Wed, 30 Dec 2020 19:27:59 +0800 Subject: [PATCH] =?UTF-8?q?P1678=20=E7=83=A6=E6=81=BC=E7=9A=84=E9=AB=98?= =?UTF-8?q?=E8=80=83=E5=BF=97=E6=84=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit R44433175 --- problem/P1678/P1678.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 problem/P1678/P1678.cpp diff --git a/problem/P1678/P1678.cpp b/problem/P1678/P1678.cpp new file mode 100644 index 00000000..4b72a9e4 --- /dev/null +++ b/problem/P1678/P1678.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + +int m, n, q, ans, a[100005]; + +int main() { + cin >> m >> n; + for (int i = 0; i < m; i++) { + cin >> a[i]; + } + sort(a, a + m); + for (int i = 0; i < n; i++) { + cin >> q; + int p = lower_bound(a, a + m, q) - a; + if (p == m) { + ans += q - a[m - 1]; + } + else if (p == 0) { + ans += a[0] - q; + } + else { + ans += min(abs(a[p] - q), abs(q - a[p - 1])); + } + } + cout << ans << endl; + return 0; +}