#include <iostream> #include <algorithm> using namespace std; bool isStable(int *a, int n) { if (n > 1) { int inv = 0; for (int i = 0; i < n - 1; i++) { for (int k = i + 1; k < n; k++) { if (a[i] > a[k]) inv++; } } for (int i = n - 1; i > 0; i--) { for (int k = i - 1; k >= 0; k--) { if (a[i] > a[k]) inv--; } } if (inv == 0) return true; } return false; } int main() { long long n, k, ctr = 0; cin >> n >> k; int a[n]; for (int i = 0; i < n; i++) a[i] = i + 1; do { if (isStable(a, n)) { ctr++; if (ctr == k) { cout << "TAK" << endl; for (int i = 0; i < n; i++) { cout << a[i] << " "; } break; } } } while (std::next_permutation(a, a + n)); if (ctr != k) { cout << "NIE" << endl; } 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 43 44 45 46 47 48 49 50 51 52 53 54 55 | #include <iostream> #include <algorithm> using namespace std; bool isStable(int *a, int n) { if (n > 1) { int inv = 0; for (int i = 0; i < n - 1; i++) { for (int k = i + 1; k < n; k++) { if (a[i] > a[k]) inv++; } } for (int i = n - 1; i > 0; i--) { for (int k = i - 1; k >= 0; k--) { if (a[i] > a[k]) inv--; } } if (inv == 0) return true; } return false; } int main() { long long n, k, ctr = 0; cin >> n >> k; int a[n]; for (int i = 0; i < n; i++) a[i] = i + 1; do { if (isStable(a, n)) { ctr++; if (ctr == k) { cout << "TAK" << endl; for (int i = 0; i < n; i++) { cout << a[i] << " "; } break; } } } while (std::next_permutation(a, a + n)); if (ctr != k) { cout << "NIE" << endl; } return 0; } |