#include <iostream> #include <string> #include <stdlib.h> #include <algorithm> #include <vector> using namespace std; int GetInversionCount(vector<int> v) { int counter = 0; for (int i = 0; i < v.size() - 1; i++) { for (int j = i + 1; j < v.size(); j++) { if (v[i] > v[j]) { counter++; } } } return counter; } bool IsStable(vector<int> v) { int x = GetInversionCount(v); reverse(v.begin(), v.end()); int y = GetInversionCount(v); return x == y; } vector<vector<int>> GetStablePermutations(vector<int> v) { vector<vector<int>> p; do { if (IsStable(v)) { p.push_back(v); } } while (next_permutation(v.begin(), v.end())); return p; } int main() { int n; unsigned long long k; cin >> n; cin >> k; vector<int> v; for (int i = 1; i <= n; i++) { v.push_back(i); } vector<vector<int>> c = GetStablePermutations(v); stable_sort(c.begin(), c.end()); if (c.size() >= k) { cout << "TAK" << "\n"; cout << c[k - 1][0]; for (int i = 1; i < n; i++) { cout << ' ' << c[k - 1][i]; } } else { cout << "NIE"; } //system("pause"); }
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | #include <iostream> #include <string> #include <stdlib.h> #include <algorithm> #include <vector> using namespace std; int GetInversionCount(vector<int> v) { int counter = 0; for (int i = 0; i < v.size() - 1; i++) { for (int j = i + 1; j < v.size(); j++) { if (v[i] > v[j]) { counter++; } } } return counter; } bool IsStable(vector<int> v) { int x = GetInversionCount(v); reverse(v.begin(), v.end()); int y = GetInversionCount(v); return x == y; } vector<vector<int>> GetStablePermutations(vector<int> v) { vector<vector<int>> p; do { if (IsStable(v)) { p.push_back(v); } } while (next_permutation(v.begin(), v.end())); return p; } int main() { int n; unsigned long long k; cin >> n; cin >> k; vector<int> v; for (int i = 1; i <= n; i++) { v.push_back(i); } vector<vector<int>> c = GetStablePermutations(v); stable_sort(c.begin(), c.end()); if (c.size() >= k) { cout << "TAK" << "\n"; cout << c[k - 1][0]; for (int i = 1; i < n; i++) { cout << ' ' << c[k - 1][i]; } } else { cout << "NIE"; } //system("pause"); } |