Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define cin(v) \
- for (auto &i : v) \
- cin >> i;
- #define cout(v) \
- for (auto &i : v) \
- cout << i << " ";
- #define br cout << '\n';
- #define ll long long
- #define ull unsigned long long
- #define all(v) v.begin(), v.end()
- #define rall(v) v.rbegin(), v.rend()
- #define MOD 1000000007
- #define Time cerr << "Time Taken: " << (float)clock() / CLOCKS_PER_SEC << " Secs" \
- << "\n";
- void Warding()
- {
- ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
- #endif
- }
- // bool is_prime(int n)
- // {
- // if (n <= 1)
- // return false;
- // for (int i = 2; i <= sqrt(n); i++)
- // if (n % i == 0)
- // return false;
- // return true;
- // }
- void solve()
- {
- ll n, k, cnt = 0;
- map<ll, ll> m, m2;
- map<pair<ll, ll>, bool> found;
- cin >> n >> k;
- vector<int> v(n);
- for (int i = 0; i < n; i++)
- {
- cin >> v[i];
- m[v[i]]++;
- m2[v[i]]++;
- }
- for (auto &i : m)
- {
- if (i.second > 1 and !found[{i.first, i.first}] and k == 0)
- {
- cnt++;
- found[{i.first, i.first}] = true;
- }
- if ((m2[i.first + k]) and k != 0 and (!found[{i.first, i.first + k}] and !found[{i.first + k, i.first}]))
- {
- cnt++;
- found[{i.first, i.first + k}] = true;
- found[{i.first + k, i.first}] = true;
- }
- if (m2[i.first - k] and k != 0 and (!found[{i.first, i.first - k}] and !found[{i.first - k, i.first}]))
- {
- cnt++;
- found[{i.first, i.first - k}] = true;
- found[{i.first - k, i.first}] = true;
- }
- }
- cout << cnt << '\n';
- }
- int main()
- {
- Warding();
- int t = 1;
- // cin >> t;
- while (t--)
- {
- solve();
- }
- Time
- }
Advertisement
Add Comment
Please, Sign In to add comment