B.cpp 521 B

1234567891011121314151617181920212223
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. //O(n*log(m))
  4. int main() {
  5. int m, n;
  6. cin >> m >> n;
  7. vector<int> a(m, 0), b(n, 0);
  8. for (auto &i:a)cin >> i;
  9. for (auto &i:b)cin >> i;
  10. sort(a.begin(), a.end());
  11. sort(b.begin(), b.end());
  12. if (m < n)swap(a, b);
  13. auto best = INT_MAX;
  14. for (auto x:a) {
  15. auto p = lower_bound(b.begin(), b.end(), x);
  16. best = min(best, abs(*p - x));
  17. if (best == 0)break;
  18. }
  19. cout << best << endl;
  20. return 0;
  21. }