fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. inline int power(int a, int b) {
  6. int x = 1;
  7. while (b) {
  8. if (b & 1) x *= a;
  9. a *= a;
  10. b >>= 1;
  11. }
  12. return x;
  13. }
  14.  
  15.  
  16. const int M = 1000000007;
  17. const int N = 3e5+9;
  18. const int INF = 2e9+1;
  19. const int LINF = 2000000000000000001;
  20.  
  21. //_ ***************************** START Below *******************************
  22.  
  23.  
  24.  
  25.  
  26. vector<int> a;
  27. void consistency(int n, int k) {
  28.  
  29. map<int,int> events;
  30.  
  31. for(int i=0; i<n; i++){
  32. int s = a[i]-k;
  33. int e = a[i]+k;
  34.  
  35. events[s]++;
  36. events[e+1]--;
  37. }
  38.  
  39. int acc = 0;
  40. int ans = 0;
  41. for(auto& e : events){
  42. acc += e.second;
  43. ans = max(ans, acc);
  44. }
  45.  
  46. cout << ans << endl;
  47.  
  48. }
  49.  
  50. void solve() {
  51.  
  52. int n, k;
  53. cin >> n >> k;
  54.  
  55. a.resize(n);
  56. for(int i=0; i<n; i++) cin >> a[i];
  57.  
  58. consistency(n, k);
  59.  
  60. }
  61.  
  62.  
  63.  
  64.  
  65.  
  66. int32_t main() {
  67. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  68.  
  69. int t = 1;
  70. cin >> t;
  71. while (t--) {
  72. solve();
  73. }
  74.  
  75. return 0;
  76. }
Success #stdin #stdout 0.01s 5324KB
stdin
1
5 2
4 7 6 9 12
stdout
3