Skip to content

Commit baa7f49

Browse files
committed
atcoder/abc181E
1 parent d66101e commit baa7f49

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

atcoder/abc181/E/main.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
using ll = int64_t;
5+
using ff = long double;
6+
7+
int N, M;
8+
vector<int> H;
9+
vector<int> W;
10+
11+
ll solve() {
12+
sort(H.begin(), H.end());
13+
sort(W.begin(), W.end());
14+
W.erase(unique(W.begin(), W.end()), W.end());
15+
16+
vector<ll> L(N, 0), R(N, 0);
17+
for (int i = 1; i < N; i += 2) {
18+
L[i] = (i-2 > 0) ? L[i-2] : 0;
19+
L[i] += H[i] - H[i-1];
20+
}
21+
for (int i = N - 2; i >= 0; i -= 2) {
22+
R[i] = (i+2 < N) ? R[i+2] : 0;
23+
R[i] += H[i+1] - H[i];
24+
}
25+
26+
ll ans = numeric_limits<ll>::max();
27+
for (int i = 0; i < N; i += 2) {
28+
ll ls = (i-1 >= 0) ? L[i-1] : 0;
29+
ll rs = (i+1 < N) ? R[i+1] : 0;
30+
auto lb = lower_bound(W.begin(), W.end(), H[i]);
31+
if (lb != W.end()) {
32+
ans = min(ans, ls + rs + (*lb - H[i]));
33+
}
34+
auto ub = upper_bound(W.begin(), W.end(), H[i]);
35+
if (ub != W.begin()) {
36+
--ub;
37+
ans = min(ans, ls + rs + (H[i] - *ub));
38+
}
39+
}
40+
41+
return ans;
42+
}
43+
44+
int main() {
45+
ios_base::sync_with_stdio(false);
46+
cin.tie(0); cout.tie(0);
47+
48+
cin >> N >> M;
49+
50+
H.assign(N, 0);
51+
for (auto &h : H) cin >> h;
52+
W.assign(M, 0);
53+
for (auto &w : W) cin >> w;
54+
cout << solve() << endl;
55+
56+
return 0;
57+
}

0 commit comments

Comments
 (0)