-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP1678.cpp
44 lines (42 loc) · 797 Bytes
/
P1678.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <bits/stdc++.h>
using namespace std;
long m, n, sum,uni;
int s[100000], t[100000];
int binSearch(int x)
{
int l = 0, r = uni-1;
while (l < r)
{
int mid = (r + l) / 2;
if (x - t[mid] <= 0)
{
r = mid;
}
else
l = mid + 1;
}
return l;
}
int main()
{
cin >> m >> n;
for (int i = 0; i < m; i++)
{
cin >> t[i];
}
for (int i = 0; i < n; i++)
{
cin >> s[i];
}
sort(t, t + m);
uni=unique(t,t+m)-t;
for (int i = 0; i < n; i++)
{
int temp = binSearch(s[i]);
int split1 = abs(s[i] - t[temp]);
int split2 = abs(temp - 1 >= 0 ? s[i] - t[temp - 1] : 1 << 30);
sum += min(split1, split2);
}
cout << sum;
return 0;
}