Advertisement
Korotkodul

LSD_v3

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