Advertisement
Korotkodul

LSD_OK

Oct 4th, 2023 (edited)
735
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.41 KB | None | 0 0
  1. #include <cmath>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. using std::cin;
  6. using std::cout;
  7. using std::max;
  8. using std::min;
  9. using std::string;
  10. using std::vector;
  11. using Ll = long long;
  12.  
  13. const int k8 = 8;
  14. const int k256 = 256;
  15. int len;
  16. vector<Ll> ar;
  17. vector<int> less_than;
  18. vector<Ll> hlp;
  19.  
  20. Ll Byte(Ll num, int par) {
  21.   Ll kk = ((1LL << k8) - 1) * (1LL << (k8 * par));
  22.   Ll res = num & kk;
  23.   Ll div = (1LL << (k8 * par));
  24.   res /= div;
  25.   return res;
  26. }
  27.  
  28. void LSD(int par) {
  29.   // cout << "GO\n";
  30.   // cout << "par = " << par << "\n";
  31.   less_than.assign(k256, 0);
  32.   for (int id = 0; id < len; ++id) {
  33.     Ll bt = Byte(ar[id], par);
  34.     // cout << "id = " << id << "\n";
  35.     // cout << "ar[id] = " << ar[id] << "\n";
  36.     // cout << "bt = " << bt << "\n";
  37.     less_than[bt]++;
  38.   }
  39.   // cout << "A\n";
  40.   int cnt = 0;
  41.   for (int id = 0; id < k256; ++id) {
  42.     int point = less_than[id];
  43.     less_than[id] = cnt;
  44.     cnt += point;
  45.   }
  46.   // cout << "B\n";
  47.   for (int id = 0; id < len; ++id) {
  48.     Ll bt = Byte(ar[id], par);
  49.     hlp[less_than[bt]] = ar[id];
  50.     less_than[bt]++;
  51.   }
  52.   ar = hlp;
  53. }
  54.  
  55. int main() {
  56.   std::ios::sync_with_stdio(false);
  57.   std::cin.tie(0);
  58.   std::cout.tie(0);
  59.   cin >> len;
  60.   ar.resize(len);
  61.   hlp.resize(len);
  62.   for (Ll& el : ar) {
  63.     cin >> el;
  64.   }
  65.   for (int par = 0; par < k8; ++par) {
  66.     LSD(par);
  67.   }
  68.   for (Ll el : ar) {
  69.     cout << el << "\n";
  70.   }
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement