/* Potyczki algorytmiczne 2016 *
* Rozwiązanie zadania tasowanie [B] *
* Piotr Maślankowski */
#include <iostream>
using namespace std;
void shuffle(int *array, int low, int high) {
if(low == high)
return;
else {
int mid = (low + high) / 2;
shuffle(array, low, mid);
shuffle(array, mid + 1, high);
int tmp[mid - low + 1];
for(int i=0; i < mid - low + 1; i++)
tmp[i] = array[low + i];
for(int i=0; i < high - mid; i++)
array[low + i] = array[mid + 1 + i];
for(int i=0; i < mid - low + 1; i++)
array[mid + 1 + i] = tmp[i];
}
}
int main() {
int n, t;
cin >> n >> t;
n = 1 << n;
int a[n];
for(int i=0; i < n; i++)
cin >> a[i];
if(t % 2 == 0) {
for(int i=0; i < n; i++)
cout << a[i] << " ";
} else {
shuffle(a, 0, n-1);
for(int i=0; i < n; i++)
cout << a[i] << " ";
}
return 0;
}
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 39 40 41 42 | /* Potyczki algorytmiczne 2016 * * Rozwiązanie zadania tasowanie [B] * * Piotr Maślankowski */ #include <iostream> using namespace std; void shuffle(int *array, int low, int high) { if(low == high) return; else { int mid = (low + high) / 2; shuffle(array, low, mid); shuffle(array, mid + 1, high); int tmp[mid - low + 1]; for(int i=0; i < mid - low + 1; i++) tmp[i] = array[low + i]; for(int i=0; i < high - mid; i++) array[low + i] = array[mid + 1 + i]; for(int i=0; i < mid - low + 1; i++) array[mid + 1 + i] = tmp[i]; } } int main() { int n, t; cin >> n >> t; n = 1 << n; int a[n]; for(int i=0; i < n; i++) cin >> a[i]; if(t % 2 == 0) { for(int i=0; i < n; i++) cout << a[i] << " "; } else { shuffle(a, 0, n-1); for(int i=0; i < n; i++) cout << a[i] << " "; } return 0; } |
English