Advertisement
Korotkodul

LSD_v4

Oct 1st, 2023
621
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.08 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.   /*cout << "from\n";
  85.   cout << "st\n";
  86.   cout << st << "\n";*/
  87.   ll res = 0;
  88.   for (int id = 0; id < 64; ++id) {
  89.     ll add = (ll)(st[id] - '0') * pow(2LL, id);
  90.     //cout << "id = " << id << "\n";
  91.     //cout << "add = " << add << "\n";
  92.     res += add;
  93.   }
  94.   return res;
  95. }
  96.  
  97.  
  98. int main() {
  99.   std::ios::sync_with_stdio(false);
  100.   std::cin.tie(0);
  101.   std::cout.tie(0);
  102.   /*
  103.   ll kk;
  104.   cin >> kk;
  105.   string st = Bin(kk);
  106.   //cout << st << "\n";
  107.   vector<string> vec = Get(st);
  108.   for (int id = 0; id < 8; ++id) {
  109.     cout << vec[id] << ' ';
  110.   } cout << "\n";
  111.   */
  112.   /*ll num;
  113.   cin >> num;
  114.   string st = Bin(num);
  115.   cout << st << "\n";
  116.   vector<string> vec = Get(st);
  117.   for (string st: vec) {
  118.     cout << st << ' ';
  119.   }
  120.   cout << "\n";
  121.   ll cmp = from(vec);
  122.   cout << cmp << "\n";*/
  123.   /*int len;
  124.   cin >> len;
  125.   vector<vector<string>> ar;
  126.   for (int id = 0; id < len; ++id) {
  127.     ll num; cin >> num;
  128.     string st = Bin(num);
  129.     vector<string> vec = Get(st);
  130.     ar.push_back(vec);
  131.   }
  132.   for (int par = 0; par < 8; ++par) {
  133.     ar = MergeSort(ar, par);
  134.   }
  135.   vector<ll>res;
  136.   for (int id = 0; id < len; ++id) {
  137.     ll num = from(ar[id]);
  138.     res.push_back(num);
  139.   }
  140.   for (int id = 0; id < len; ++id) {
  141.     cout << res[id] << "\n";
  142.   }
  143.   cout << "\n";*/
  144. }
  145. /*
  146. 9223372036854775807
  147.  
  148. 111111111111111111111111111111111111111111111111111111111111111
  149. 1111111111111111111111111111111111111111111111111111111111111110
  150.  
  151. */
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement