Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <iostream>
- #include <string>
- #include <vector>
- using std::cin;
- using std::cout;
- using std::max;
- using std::min;
- using std::string;
- using std::vector;
- using Ll = long long;
- const auto k8 = 8;
- const auto k64 = 64;
- Ll Amount(string st) {
- Ll res = 0;
- for (int id = 0; id < k8; ++id) {
- Ll add = (Ll)(st[id] - '0') * pow(2LL, id);
- res += add;
- }
- return res;
- }
- vector<vector<string>> MergeSort(vector<vector<string>> ar, int par) {
- int len = ar.size();
- if (len == 1) {
- return ar;
- }
- vector<vector<string>> lt;
- vector<vector<string>> rt;
- for (int id = 0; id < len / 2; ++id) {
- lt.push_back(ar[id]);
- }
- for (int id = len / 2; id < len; ++id) {
- rt.push_back(ar[id]);
- }
- lt = MergeSort(lt, par);
- rt = MergeSort(rt, par);
- int ln = lt.size();
- int rn = rt.size();
- int lid = 0;
- int rid = 0;
- vector<vector<string>> res;
- while (lid < ln || rid < rn) {
- if (lid < ln && rid < rn) {
- if (Amount(lt[lid][par]) <= Amount(rt[rid][par])) {
- res.push_back(lt[lid]);
- lid++;
- } else {
- res.push_back(rt[rid]);
- rid++;
- }
- } else if (lid < ln) {
- res.push_back(lt[lid]);
- lid++;
- } else if (rid < rn) {
- res.push_back(rt[rid]);
- rid++;
- }
- }
- return res;
- }
- string Bin(Ll num) {
- string st;
- while (num > 0) {
- st += std::to_string(num % 2);
- num /= 2;
- }
- while ((int)st.size() < k64) {
- st += '0';
- }
- return st;
- }
- vector<string> Get(string st) {
- vector<string> res;
- for (int id = 0; id < k64; id += k8) {
- string add = st.substr(id, k8);
- res.push_back(add);
- }
- return res;
- }
- Ll From(vector<string> vec) {
- string st;
- for (int id = 0; id < k8; ++id) {
- st += vec[id];
- }
- Ll res = 0;
- for (int id = 0; id < k64; ++id) {
- Ll add = (Ll)(st[id] - '0') * pow(2LL, id);
- res += add;
- }
- return res;
- }
- int main() {
- std::ios::sync_with_stdio(false);
- std::cin.tie(0);
- std::cout.tie(0);
- int len;
- cin >> len;
- vector<vector<string>> ar;
- for (int id = 0; id < len; ++id) {
- Ll num;
- cin >> num;
- string st = Bin(num);
- vector<string> vec = Get(st);
- ar.push_back(vec);
- }
- for (int par = 0; par < k8; ++par) {
- ar = MergeSort(ar, par);
- }
- vector<Ll> res;
- for (int id = 0; id < len; ++id) {
- Ll num = From(ar[id]);
- res.push_back(num);
- }
- for (int id = 0; id < len; ++id) {
- cout << res[id] << "\n";
- }
- }
- /*
- 9223372036854775807
- 111111111111111111111111111111111111111111111111111111111111111
- 1111111111111111111111111111111111111111111111111111111111111110
- 5
- 9244894871689
- 9521193378807
- 1748929606960
- 1006499032553
- 3289527480072
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement