Advertisement
Korotkodul

LSD_v1

Oct 2nd, 2023 (edited)
865
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 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 k20 = 20;
  14. const int k10 = 10;
  15. int len;
  16. vector<Ll> ar;
  17. vector<int> less_than;
  18. vector<Ll> hlp;
  19.  
  20. int Digit(Ll num, int par) {
  21.   Ll mod = pow(k10, par + 1);
  22.   Ll div = pow(k10, par);
  23.   return num % mod / div;
  24. }
  25.  
  26. void LSD(int par) {
  27.   less_than.assign(k10, 0);
  28.   for (int id = 0; id < len; ++id) {
  29.     int dig = Digit(ar[id], par);
  30.     less_than[dig]++;
  31.   }
  32.   int cnt = 0;
  33.   for (int id = 0; id < k10; ++id) {
  34.     int point = less_than[id];
  35.     less_than[id] = cnt;
  36.     cnt += point;
  37.   }
  38.   for (int id = 0; id < len; ++id) {
  39.     int dig = Digit(ar[id], par);
  40.     hlp[less_than[dig]] = ar[id];
  41.     less_than[dig]++;
  42.   }
  43.   ar = hlp;
  44. }
  45.  
  46. int main() {
  47.   /*std::ios::sync_with_stdio(false);
  48.   std::cin.tie(0);
  49.   std::cout.tie(0);*/
  50.   cin >> len;
  51.   ar.resize(len);
  52.   hlp.resize(len);
  53.   for (Ll& el : ar) {
  54.     cin >> el;
  55.   }
  56.   for (int par = 0; par < k20; ++par) {
  57.     LSD(par);
  58.   }
  59.   for (Ll el : ar) {
  60.     cout << el << "\n";
  61.   }
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement