fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int Mod=998244353;
  5.  
  6. bool f(int mid, vector<int> &towers, vector<int> &cities){
  7. for (int city : cities) {
  8. auto it = lower_bound(towers.begin(), towers.end(), city);
  9.  
  10. int best = 1e9; // very large
  11.  
  12. if (it != towers.end()) best = min(best, abs(city - *it)); // tower >= city
  13. if (it != towers.begin()) best = min(best, abs(city - *(--it))); // tower < city
  14.  
  15. if (best > mid) return false;
  16. }
  17. return true;
  18. }
  19.  
  20. void solve() {
  21. int n,m;
  22. cin >> n >> m;
  23. vector<int> cities(n), towers(m);
  24. for(int i=0;i<n;i++) cin >> cities[i];
  25. for(int i=0;i<m;i++) cin >> towers[i];
  26.  
  27. sort(cities.begin(), cities.end());
  28. sort(towers.begin(), towers.end());
  29.  
  30. int low = 0, high = 1e9, ans = 0;
  31. while(low <= high){
  32. int mid = (low + high) / 2;
  33. if(f(mid, towers, cities)){
  34. ans = mid;
  35. high = mid-1;
  36. }
  37. else{
  38. low = mid+1;
  39. }
  40. }
  41. cout << ans << '\n';
  42. }
  43.  
  44.  
  45. int main(){
  46. ios::sync_with_stdio(false);
  47. cin.tie(nullptr);
  48.  
  49. /*int t;
  50.   cin >> t;
  51.   while (t--)*/ solve();
  52.  
  53.  
  54. return 0;
  55. }
Success #stdin #stdout 0s 5308KB
stdin
5 3
1 5 10 14 17
4 11 15
stdout
3