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. // 9 4
  25. // 1 5 0 3 4 1 2 2 3
  26.  
  27.  
  28.  
  29. vector<int> a;
  30. int consistency(int n, int k) {
  31.  
  32. int ans = 0;
  33. unordered_map<int,int> freq;
  34. for(int i=0; i<n; i++){
  35. if(freq.count(k-a[i])){
  36. ans += freq[k-a[i]];
  37. }
  38. freq[a[i]]++;
  39. }
  40.  
  41. return ans;
  42. }
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58. int practice(int n, int k) {
  59.  
  60. int ans = 0;
  61.  
  62.  
  63. return ans;
  64. }
  65.  
  66. void solve() {
  67.  
  68. int n, k;
  69. cin >> n >> k;
  70. a.resize(n);
  71. for(int i=0; i<n; i++) cin >> a[i];
  72.  
  73. cout << consistency(n, k) << endl ;
  74.  
  75. // cout << consistency(n, k) << " -> " << practice(n, k) << endl ;
  76. }
  77.  
  78.  
  79.  
  80.  
  81.  
  82. int32_t main() {
  83. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  84.  
  85. int t = 1;
  86. while (t--) {
  87. solve();
  88. }
  89.  
  90. return 0;
  91. }
Success #stdin #stdout 0.01s 5320KB
stdin
9 4
1 5 0 3 4 1 2 2 3
stdout
6