This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
const int maxn = 1e5 + 5;
int n, m;
int a[maxn], b[maxn];
vector<int> g[maxn];
int par[maxn];
int find(int u) {
return par[u] == u ? u : par[u] = find(par[u]);
}
bool can(int del) {
for(int i = 1; i <= m; i++) {
g[i].clear();
}
for(int i = 1; i <= n; i++) {
int l = lower_bound(b + 1, b + 1 + m, a[i] - del) - b;
int r = upper_bound(b + 1, b + 1 + m, a[i] + del) - b - 1;
g[r].emplace_back(l);
}
iota(par, par + 1 + m + 1, 0);
for(int r = 1; r <= m; r++) {
sort(g[r].begin(), g[r].end(), [](int x, int y){
return x > y;
});
for(int l : g[r]) {
int mn = find(l);
if(mn > r) {
return 0;
} else {
par[mn] = mn + 1;
}
}
} return 1;
}
int main(int argc, char const *argv[])
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
cin >> n >> m;
if(n <= m) {
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
for(int i = 1; i <= m; i++) {
cin >> b[i];
}
} else {
for(int i = 1; i <= n; i++) {
cin >> b[i];
}
for(int i = 1; i <= m; i++) {
cin >> a[i];
}
swap(n, m);
}
sort(a + 1, a + 1 + n);
sort(b + 1, b + 1 + m);
int lo = 0, hi = 1e9, opt = -1, mid;
while(lo <= hi) {
mid = lo + hi >> 1;
if(can(mid)) {
opt = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
cout << opt << endl;
return 0;
}
Compilation message (stderr)
cipele.cpp: In function 'int main(int, const char**)':
cipele.cpp:68:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
mid = lo + hi >> 1;
~~~^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |