Advertisement
Korotkodul

CF_TL=test15

Aug 13th, 2023 (edited)
1,054
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6. vector <int> a;
  7. vector <bool> used;
  8. int n,k;
  9.  
  10. bool sh  = 0;
  11.  
  12. int get(int i) {
  13.     int cnt = a[i];
  14.     int amount = 1;
  15.  
  16.     if (sh) {
  17.         cout << "i = " << i << "\n";
  18.     }
  19.  
  20.     for (int j = i + 1; j < n; ++j) {
  21.         if (used[j]) {
  22.             continue;
  23.         }
  24.         if (abs(a[j] - cnt) <= k) {
  25.             used[j] = 1;
  26.             cnt = a[j];
  27.             amount++;
  28.         }
  29.     }
  30.  
  31.     /*cout << "amount = " << amount << "\n";
  32.     cout << "used\n";
  33.     for (bool i: used) {
  34.         cout << i << ' ' ;
  35.     } cout << "\n";*/
  36.  
  37.     return amount;
  38. }
  39.  
  40. int slv() {
  41.     int mx = 0;
  42.     used.assign(n, 0);
  43.     for (int i = 0; i < n; ++i) {
  44.         if (used[i]) {
  45.             continue;
  46.         }
  47.         used[i] = 1;
  48.         int now = get(i);
  49.         mx = max(mx, now);
  50.     }
  51.     return n - mx;
  52. }
  53.  
  54. /*
  55. 8 1
  56. 8 3 1 4 5 10 7 3
  57. */
  58.  
  59. int main()
  60. {
  61.     ios::sync_with_stdio(0);
  62.     cin.tie(0);
  63.     int t=1;
  64.     cin >> t;
  65.     for (int go = 0; go < t; ++go) {
  66.         cin >> n >> k;
  67.         a.resize(n);
  68.         for (int &i: a) cin >> i;
  69.         sort(a.begin(), a.end());
  70.         /*if (sh) {
  71.             cout << "n k a\n";
  72.             cout << n << ' ' << k << "\n";
  73.             for (int i: a) cout << i << ' ';
  74.             cout << "\n";
  75.         }*/
  76.  
  77.         int ans = slv();
  78.         cout << ans << "\n";
  79.     }
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement