#include <iostream> #include <string> #include <vector> long long n; unsigned long long k; unsigned long long found; bool end; bool stable(std::vector<long> x) { if(x.size()<3) return true; int inverts1 = 0; int inverts2 = 0; for(long i=0; i<x.size(); i++) { for(long j=i; j<x.size(); j++) { if (x.at(i) > x.at(j)) { inverts1++; } } } for(long i=x.size()-1; i>=0; i--) { for(long j=i; j>=0; j--) { if (x.at(i) > x.at(j)) { inverts2++; } } } return inverts1==inverts2; } void generate(std::vector<long> x) { for(long i=1; i<=n && !end; i++) { bool fail = false; for(long j = 0; j<x.size(); j++) { if (x.at(j) == i) { fail = true; break; } } if(fail) continue; else { std::vector<long> newx = x; newx.push_back(i); generate(newx); } } if(x.size() == n && stable(x)) { found++; if (found == k) { std::cout << "TAK" << std::endl; for (long i = 0; i < x.size(); i++) { std::cout << x.at(i) <<" "; } std::cout << std::endl; end = true; } } } int main() { end = false; std::cin >> n; std::cin >> k; found = 0; std::vector<long> v; generate(v); if(!end) 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 76 77 78 79 80 81 82 83 84 | #include <iostream> #include <string> #include <vector> long long n; unsigned long long k; unsigned long long found; bool end; bool stable(std::vector<long> x) { if(x.size()<3) return true; int inverts1 = 0; int inverts2 = 0; for(long i=0; i<x.size(); i++) { for(long j=i; j<x.size(); j++) { if (x.at(i) > x.at(j)) { inverts1++; } } } for(long i=x.size()-1; i>=0; i--) { for(long j=i; j>=0; j--) { if (x.at(i) > x.at(j)) { inverts2++; } } } return inverts1==inverts2; } void generate(std::vector<long> x) { for(long i=1; i<=n && !end; i++) { bool fail = false; for(long j = 0; j<x.size(); j++) { if (x.at(j) == i) { fail = true; break; } } if(fail) continue; else { std::vector<long> newx = x; newx.push_back(i); generate(newx); } } if(x.size() == n && stable(x)) { found++; if (found == k) { std::cout << "TAK" << std::endl; for (long i = 0; i < x.size(); i++) { std::cout << x.at(i) <<" "; } std::cout << std::endl; end = true; } } } int main() { end = false; std::cin >> n; std::cin >> k; found = 0; std::vector<long> v; generate(v); if(!end) std::cout<<"NIE"<<std::endl; return 0; } |