fork download
  1. #include <bits/stdc++.h>
  2. #define NMAX 10005
  3. using namespace std;
  4. int n , k;
  5. int Male[NMAX] , Female[NMAX];
  6.  
  7. long long ans ;
  8.  
  9. void process()
  10. {
  11. cin>> n >> k;
  12. for(int i = 1 ; i <= n ; i++)
  13. {
  14. int height;
  15. cin>>height;
  16.  
  17. if(height > 0) Male[height]++;
  18.  
  19. if(height <= 0) Female[-height]++;
  20. }
  21.  
  22. for(int i = 1 ; i < NMAX ; i++) Male[i] += Male[i-1];
  23.  
  24. ans += 1ll * Female[0] * Male[k];
  25.  
  26. for(int height = 1 ; height < NMAX - k ; height++)
  27. {
  28. ans += 1ll * Female[height] * (Male[height + k] - Male[height - 1]);
  29. }
  30.  
  31. cout<<ans;
  32. }
  33. int main()
  34. {
  35. ios_base::sync_with_stdio(0);
  36. cin.tie(nullptr);
  37. cout.tie(nullptr);
  38. process();
  39. return 0;
  40. }
  41.  
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
Standard output is empty