IMohammedNasr

Untitled

Apr 21st, 2022
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define cin(v)        \
  4.     for (auto &i : v) \
  5.         cin >> i;
  6. #define cout(v)       \
  7.     for (auto &i : v) \
  8.         cout << i << " ";
  9. #define br cout << '\n';
  10. #define ll long long
  11. #define ull unsigned long long
  12. #define all(v) v.begin(), v.end()
  13. #define rall(v) v.rbegin(), v.rend()
  14. #define MOD 1000000007
  15. #define Time cerr << "Time Taken: " << (float)clock() / CLOCKS_PER_SEC << " Secs" \
  16.                   << "\n";
  17.  
  18. void Warding()
  19. {
  20.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  21. #ifndef ONLINE_JUDGE
  22.     freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  23. #endif
  24. }
  25. // bool is_prime(int n)
  26. // {
  27. //     if (n <= 1)
  28. //         return false;
  29. //     for (int i = 2; i <= sqrt(n); i++)
  30. //         if (n % i == 0)
  31. //             return false;
  32. //     return true;
  33. // }
  34.  
  35. void solve()
  36. {
  37.     ll n, k, cnt = 0;
  38.     map<ll, ll> m, m2;
  39.     map<pair<ll, ll>, bool> found;
  40.     cin >> n >> k;
  41.     vector<int> v(n);
  42.     for (int i = 0; i < n; i++)
  43.     {
  44.         cin >> v[i];
  45.         m[v[i]]++;
  46.         m2[v[i]]++;
  47.     }
  48.     for (auto &i : m)
  49.     {
  50.         if (i.second > 1 and !found[{i.first, i.first}] and k == 0)
  51.         {
  52.             cnt++;
  53.             found[{i.first, i.first}] = true;
  54.         }
  55.         if ((m2[i.first + k]) and k != 0 and (!found[{i.first, i.first + k}] and !found[{i.first + k, i.first}]))
  56.         {
  57.             cnt++;
  58.             found[{i.first, i.first + k}] = true;
  59.             found[{i.first + k, i.first}] = true;
  60.         }
  61.         if (m2[i.first - k] and k != 0 and (!found[{i.first, i.first - k}] and !found[{i.first - k, i.first}]))
  62.         {
  63.             cnt++;
  64.             found[{i.first, i.first - k}] = true;
  65.             found[{i.first - k, i.first}] = true;
  66.         }
  67.     }
  68.     cout << cnt << '\n';
  69. }
  70.  
  71. int main()
  72. {
  73.     Warding();
  74.     int t = 1;
  75.     // cin >> t;
  76.     while (t--)
  77.     {
  78.         solve();
  79.     }
  80.     Time
  81. }
Advertisement
Add Comment
Please, Sign In to add comment