Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- vector <int> a;
- vector <bool> used;
- int n,k;
- bool sh = 0;
- int get(int i) {
- int cnt = a[i];
- int amount = 1;
- if (sh) {
- cout << "i = " << i << "\n";
- }
- for (int j = i + 1; j < n; ++j) {
- if (used[j]) {
- continue;
- }
- if (abs(a[j] - cnt) <= k) {
- used[j] = 1;
- cnt = a[j];
- amount++;
- }
- }
- /*cout << "amount = " << amount << "\n";
- cout << "used\n";
- for (bool i: used) {
- cout << i << ' ' ;
- } cout << "\n";*/
- return amount;
- }
- int slv() {
- int mx = 0;
- used.assign(n, 0);
- for (int i = 0; i < n; ++i) {
- if (used[i]) {
- continue;
- }
- used[i] = 1;
- int now = get(i);
- mx = max(mx, now);
- }
- return n - mx;
- }
- /*
- 8 1
- 8 3 1 4 5 10 7 3
- */
- int main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- int t=1;
- cin >> t;
- for (int go = 0; go < t; ++go) {
- cin >> n >> k;
- a.resize(n);
- for (int &i: a) cin >> i;
- sort(a.begin(), a.end());
- /*if (sh) {
- cout << "n k a\n";
- cout << n << ' ' << k << "\n";
- for (int i: a) cout << i << ' ';
- cout << "\n";
- }*/
- int ans = slv();
- cout << ans << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement