fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef pair<int, int> ii;
  7.  
  8. const int INF = 1e9;
  9. const ll LINF = 1e18;
  10.  
  11. const int N = 2e5 + 5;
  12.  
  13. int n, m, k;
  14. int a[N], b[N];
  15.  
  16. int main() {
  17. ios::sync_with_stdio(false);
  18. cin.tie(nullptr);
  19. cin >> n >> m >> k;
  20. for (int i = 1; i <= n; i++) cin >> a[i];
  21. for (int i = 1; i <= m; i++) cin >> b[i];
  22.  
  23. sort(a + 1, a + n + 1);
  24. sort(b + 1, b + m + 1);
  25.  
  26. int ans = 0;
  27. for (int i = 1, j = 1; i <= n; i++) {
  28. // j là vị trí nhỏ nhất thoả mãn b[j] >= a[i] - k
  29. while (j <= m && b[j] < a[i] - k) j++;
  30. if (j <= m && b[j] <= a[i] + k) {
  31. ans++;
  32. j++;
  33. }
  34. }
  35.  
  36. cout << ans << '\n';
  37. }
Success #stdin #stdout 0.01s 5288KB
stdin
4 3 5
60 45 80 60
30 60 75
stdout
2