#include <bits/stdc++.h>
#define pb push_back
#define eb emplace_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define uniq(x) x.erase(unique(all(x)), x.end())
#define rall(x) x.rbegin(), x.rend()
#define each(x, v) for(auto &x : v)
#define mp make_pair
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using str = string;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 5;
void setIO() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
int32_t main() {
setIO();
int n, m;
cin >> n >> m;
vector<int> a(n), b(m);
for(int &x : a) cin >> x;
for(int &x : b) cin >> x;
sort(all(a));
sort(all(b));
if(n == m) {
int ans = 0;
for(int i=0; i<n; i++)
ans = max(ans, abs(a[i] - b[i]));
cout << ans << '\n';
} else {
if(n > m) swap(a, b);
int ans = 1e9;
int l=0, r=max(b.back() - a[0], a.back() - b[0]);
while(l <= r) {
int mid = (l + r) / 2;
int i=0, j=0, pairs = 0;
//cout << mid
while(i < min(n, m) && j < max(n, m)) {
if(abs(a[i] - b[j]) <= mid) {
pairs++, i++, j++;
} else if(a[i] - b[j] > mid) {
j++;
} else if(a[i] - b[j] < mid) {
i++;
}
}
if(pairs == min(n, m)) {
ans = mid;
r = mid - 1;
} else if(pairs < min(n, m)) {
l = mid + 1;
}
}
cout << ans << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
2648 KB |
Output is correct |
2 |
Correct |
28 ms |
2892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
2900 KB |
Output is correct |
2 |
Correct |
29 ms |
2888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
400 KB |
Output is correct |
2 |
Correct |
2 ms |
464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
2384 KB |
Output is correct |
2 |
Correct |
19 ms |
1748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
2604 KB |
Output is correct |
2 |
Correct |
14 ms |
1924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
2260 KB |
Output is correct |
2 |
Correct |
27 ms |
2524 KB |
Output is correct |