fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6. inline int power(int a, int b) {
  7. int x = 1;
  8. while (b) {
  9. if (b & 1) x *= a;
  10. a *= a;
  11. b >>= 1;
  12. }
  13. return x;
  14. }
  15.  
  16.  
  17. const int M = 1000000007;
  18. const int N = 3e5+9;
  19. const int INF = 2e9+1;
  20. const int LINF = 2000000000000000001;
  21.  
  22. //_ ***************************** START Below *******************************
  23.  
  24. vector<int> a;
  25. vector<int> b;
  26.  
  27.  
  28. bool isPossible(int n, int m, int mid){
  29.  
  30. for(int i=0; i<n; i++){
  31. int val = a[i];
  32. int minR = a[i]-mid;
  33. int maxR = a[i]+mid;
  34.  
  35. auto maxCt = lower_bound(begin(b), end(b), maxR) - begin(b);
  36. auto minCt = lower_bound(begin(b), end(b), minR) - begin(b);
  37.  
  38. if((maxCt<m && b[maxCt] == maxR) || (minCt<m && b[minCt] == minR)) {
  39. continue;
  40. }
  41.  
  42. if(maxCt - minCt <= 0) return false;
  43.  
  44. }
  45.  
  46. return true;
  47. }
  48.  
  49. int consistency(int n, int m){
  50.  
  51. int s = 0, e = INF;
  52. int ans = INF;
  53. while(s<=e){
  54. int mid = s + (e-s)/2;
  55.  
  56. if(isPossible(n, m, mid)){
  57. ans = min(ans, mid);
  58. e = mid-1;
  59. }
  60. else s = mid+1;
  61. }
  62.  
  63. return ans;
  64.  
  65. }
  66.  
  67. void solve() {
  68.  
  69. int n, m;
  70. cin>>n>>m;
  71. a.resize(n);
  72. b.resize(m);
  73.  
  74. for(int i=0; i<n; i++) cin >> a[i];
  75. for(int i=0; i<m; i++) cin >> b[i];
  76. cout << consistency(n, m) << endl;
  77.  
  78.  
  79. }
  80.  
  81.  
  82.  
  83.  
  84.  
  85. int32_t main() {
  86. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  87.  
  88. int t = 1;
  89. cin >> t;
  90. while (t--) {
  91. solve();
  92. }
  93.  
  94. return 0;
  95. }
Success #stdin #stdout 0.01s 5316KB
stdin
2
3 2
-2 2 4
-3 0
5 3
1 5 10 14 17
4 11 15
stdout
4
3