Beingamanforever

stable_sort idea

Dec 25th, 2024
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. /**
  2.  *    author:  compounding
  3.  *    created: 2024-12-25 16:12:44
  4.  **/
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
  8. #define NeedForSpeed                  \
  9.     ios_base::sync_with_stdio(false); \
  10.     cin.tie(NULL);                    \
  11.     cout.tie(NULL);
  12. #define int long long
  13. #define all(x) (x).begin(), (x).end()
  14. typedef vector<int> vi;
  15. typedef vector<bool> vb;
  16. typedef vector<vi> vvi;
  17. typedef vector<pair<int, int>> vpi;
  18. #define f first
  19. #define s second
  20. #define yes cout << "YES" << endl
  21. #define no cout << "NO" << endl
  22. #define endl "\n"
  23. const int mod = 1000000007;
  24. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  25.  
  26. void solve()
  27. {
  28.     int n, k;
  29.     cin >> n >> k;
  30.     vpi a(n);
  31.     for (int i = 0; i < n; i++)
  32.     {
  33.         cin >> a[i].f;
  34.         a[i].f %= k;
  35.         if (!a[i].f)
  36.         {
  37.             a[i].f = k;
  38.         }
  39.         a[i].s = i;
  40.     }
  41.     // stable_sort to maintain the order of the elements (smaller index first)
  42.     stable_sort(all(a), [&](pair<int, int> i, pair<int, int> j)
  43.                 { return i.f > j.f; });
  44.     for (auto x : a)
  45.     {
  46.         cout << x.s + 1 << " ";
  47.     }
  48.     cout << endl;
  49. }
  50.  
  51. signed main()
  52. {
  53.     NeedForSpeed;
  54.     int t = 1;
  55.     cin >> t;
  56.     while (t--)
  57.     {
  58.         solve();
  59.     }
  60.     return 0;
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment