Advertisement
Korotkodul

LSD_v5_TL

Oct 1st, 2023 (edited)
803
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1. #include <cmath>
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using std::cin;
  7. using std::cout;
  8. using std::max;
  9. using std::min;
  10. using std::string;
  11. using std::vector;
  12. using Ll = long long;
  13. const auto k8 = 8;
  14. const auto k64 = 64;
  15.  
  16. Ll Amount(string st) {
  17.   Ll res = 0;
  18.   for (int id = 0; id < k8; ++id) {
  19.     Ll add = (Ll)(st[id] - '0') * pow(2LL, id);
  20.     res += add;
  21.   }
  22.   return res;
  23. }
  24.  
  25. vector<vector<string>> MergeSort(vector<vector<string>> ar, int par) {
  26.   int len = ar.size();
  27.   if (len == 1) {
  28.     return ar;
  29.   }
  30.   vector<vector<string>> lt;
  31.   vector<vector<string>> rt;
  32.   for (int id = 0; id < len / 2; ++id) {
  33.     lt.push_back(ar[id]);
  34.   }
  35.   for (int id = len / 2; id < len; ++id) {
  36.     rt.push_back(ar[id]);
  37.   }
  38.   lt = MergeSort(lt, par);
  39.   rt = MergeSort(rt, par);
  40.   int ln = lt.size();
  41.   int rn = rt.size();
  42.   int lid = 0;
  43.   int rid = 0;
  44.   vector<vector<string>> res;
  45.   while (lid < ln || rid < rn) {
  46.     if (lid < ln && rid < rn) {
  47.       if (Amount(lt[lid][par]) <= Amount(rt[rid][par])) {
  48.         res.push_back(lt[lid]);
  49.         lid++;
  50.       } else {
  51.         res.push_back(rt[rid]);
  52.         rid++;
  53.       }
  54.     } else if (lid < ln) {
  55.       res.push_back(lt[lid]);
  56.       lid++;
  57.  
  58.     } else if (rid < rn) {
  59.       res.push_back(rt[rid]);
  60.       rid++;
  61.     }
  62.   }
  63.   return res;
  64. }
  65.  
  66. string Bin(Ll num) {
  67.   string st;
  68.   while (num > 0) {
  69.     st += std::to_string(num % 2);
  70.     num /= 2;
  71.   }
  72.   while ((int)st.size() < k64) {
  73.     st += '0';
  74.   }
  75.   return st;
  76. }
  77.  
  78. vector<string> Get(string st) {
  79.   vector<string> res;
  80.   for (int id = 0; id < k64; id += k8) {
  81.     string add = st.substr(id, k8);
  82.     res.push_back(add);
  83.   }
  84.   return res;
  85. }
  86.  
  87. Ll From(vector<string> vec) {
  88.   string st;
  89.   for (int id = 0; id < k8; ++id) {
  90.     st += vec[id];
  91.   }
  92.   Ll res = 0;
  93.   for (int id = 0; id < k64; ++id) {
  94.     Ll add = (Ll)(st[id] - '0') * pow(2LL, id);
  95.     res += add;
  96.   }
  97.   return res;
  98. }
  99.  
  100. int main() {
  101.   std::ios::sync_with_stdio(false);
  102.   std::cin.tie(0);
  103.   std::cout.tie(0);
  104.   int len;
  105.   cin >> len;
  106.   vector<vector<string>> ar;
  107.   for (int id = 0; id < len; ++id) {
  108.     Ll num;
  109.     cin >> num;
  110.     string st = Bin(num);
  111.     vector<string> vec = Get(st);
  112.     ar.push_back(vec);
  113.   }
  114.   for (int par = 0; par < k8; ++par) {
  115.     ar = MergeSort(ar, par);
  116.   }
  117.   vector<Ll> res;
  118.   for (int id = 0; id < len; ++id) {
  119.     Ll num = From(ar[id]);
  120.     res.push_back(num);
  121.   }
  122.   for (int id = 0; id < len; ++id) {
  123.     cout << res[id] << "\n";
  124.   }
  125. }
  126. /*
  127. 9223372036854775807
  128.  
  129. 111111111111111111111111111111111111111111111111111111111111111
  130. 1111111111111111111111111111111111111111111111111111111111111110
  131.  
  132. 5
  133. 9244894871689
  134. 9521193378807
  135. 1748929606960
  136. 1006499032553
  137. 3289527480072
  138. */
  139.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement