#include <iostream> #include <string> #include <map> using namespace std; int cards[2 << 20] = {}; void sort(int beg, int end, int n){ if(beg + 2== end){ if(n % 2){ std::swap(cards[beg], cards[beg +1]); } return; } sort(beg, beg + (end-beg) / 2, n); sort(beg + (end-beg) / 2, end, n); if(n % 2){ for(int i = 0; i < ((end - beg) / 2); i++){ std::swap(cards[beg + i], cards[beg + ((end-beg)/2)+i]); } } } int main(){ int t; int n; cin >> n >> t ; for(int i = 0; i < (1 << n); i++){ cin >> cards[i]; } sort(0, 1 << n, t); for(int i = 0; i < (1 << n); i++){ cout << cards[i] << " "; } cout << endl; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #include <iostream> #include <string> #include <map> using namespace std; int cards[2 << 20] = {}; void sort(int beg, int end, int n){ if(beg + 2== end){ if(n % 2){ std::swap(cards[beg], cards[beg +1]); } return; } sort(beg, beg + (end-beg) / 2, n); sort(beg + (end-beg) / 2, end, n); if(n % 2){ for(int i = 0; i < ((end - beg) / 2); i++){ std::swap(cards[beg + i], cards[beg + ((end-beg)/2)+i]); } } } int main(){ int t; int n; cin >> n >> t ; for(int i = 0; i < (1 << n); i++){ cin >> cards[i]; } sort(0, 1 << n, t); for(int i = 0; i < (1 << n); i++){ cout << cards[i] << " "; } cout << endl; } |