#include <iostream> #include <vector> #include <algorithm> struct IncGenerator { int current_; IncGenerator(int start) : current_(start) {} int operator() () { return current_++; } }; int countInversions(std::vector<int> permutation) { int inverstionCnt = 0; for (auto i = 0; i < permutation.size(); i++) { for (auto j = i + 1; j < permutation.size(); j++) { if (permutation[i] > permutation[j]) { inverstionCnt++; } } } return inverstionCnt; } std::vector<int> getReversedPermutation(std::vector<int> permutation) { std::reverse(permutation.begin(), permutation.end()); return permutation; } bool checkIfInversionsAreEqualWithReversed(std::vector<int>& permutation) { return countInversions(permutation) == countInversions(getReversedPermutation(permutation)); } std::vector<int> generateFirstPermutation(int n) { std::vector<int> v(n); IncGenerator g(1); std::generate(v.begin(), v.end(), g); return v; } int main() { int n; int k; std::cin >> n >> k; auto permutation = generateFirstPermutation(n); int permutationCnt = 0; while (permutationCnt != k && std::next_permutation(permutation.begin(), permutation.end())) { if (checkIfInversionsAreEqualWithReversed(permutation)) { permutationCnt++; } }; if (permutationCnt == k) { std::cout << "TAK" << std::endl; for (auto el : permutation) std::cout << el << " "; std::cout << std::endl; } else { std::cout << "NIE" << std::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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | #include <iostream> #include <vector> #include <algorithm> struct IncGenerator { int current_; IncGenerator(int start) : current_(start) {} int operator() () { return current_++; } }; int countInversions(std::vector<int> permutation) { int inverstionCnt = 0; for (auto i = 0; i < permutation.size(); i++) { for (auto j = i + 1; j < permutation.size(); j++) { if (permutation[i] > permutation[j]) { inverstionCnt++; } } } return inverstionCnt; } std::vector<int> getReversedPermutation(std::vector<int> permutation) { std::reverse(permutation.begin(), permutation.end()); return permutation; } bool checkIfInversionsAreEqualWithReversed(std::vector<int>& permutation) { return countInversions(permutation) == countInversions(getReversedPermutation(permutation)); } std::vector<int> generateFirstPermutation(int n) { std::vector<int> v(n); IncGenerator g(1); std::generate(v.begin(), v.end(), g); return v; } int main() { int n; int k; std::cin >> n >> k; auto permutation = generateFirstPermutation(n); int permutationCnt = 0; while (permutationCnt != k && std::next_permutation(permutation.begin(), permutation.end())) { if (checkIfInversionsAreEqualWithReversed(permutation)) { permutationCnt++; } }; if (permutationCnt == k) { std::cout << "TAK" << std::endl; for (auto el : permutation) std::cout << el << " "; std::cout << std::endl; } else { std::cout << "NIE" << std::endl; } return 0; } |