#include <iostream> #include <algorithm> #include <vector> #include <numeric> #include <iterator> using namespace std; bool is_stable(vector<long> v, long n); long getInvCount(vector<long> v, long maxElement); int main() { unsigned long long k; unsigned long n; cin >> n >> k; std::vector<long> v(n) ; std::iota (std::begin(v), std::end(v), 1); do { if(is_stable(v, n)) k--; if(!k){ cout << "TAK" << endl; copy(v.begin(), v.end(), ostream_iterator<long>(cout, " ")); return 0; } } while(next_permutation(v.begin(),v.end())); cout << "NIE"; return 0; } bool is_stable(vector<long> v, long n){ vector<long> rev(n); reverse_copy(begin(v), end(v), begin(rev)); return getInvCount(v,n) == getInvCount(rev,n); } long getSum(long BITree[], long index){ long sum = 0; while (index > 0) { sum += BITree[index]; index -= index & (-index); } return sum; } void updateBIT(long BITree[], long n, long index, long val){ while (index <= n) { BITree[index] += val; index += index & (-index); } } long getInvCount(vector<long> v, long maxElement){ long invcount = 0; vector<long> BIT(maxElement+1, 0); for (long i=v.size()-1; i>=0; i--) { invcount += getSum(BIT.data(), v[i]-1); // accumulate(BIT.begin(), vector.end(), 0); updateBIT(BIT.data(), maxElement, v[i], 1); } return invcount; }
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 | #include <iostream> #include <algorithm> #include <vector> #include <numeric> #include <iterator> using namespace std; bool is_stable(vector<long> v, long n); long getInvCount(vector<long> v, long maxElement); int main() { unsigned long long k; unsigned long n; cin >> n >> k; std::vector<long> v(n) ; std::iota (std::begin(v), std::end(v), 1); do { if(is_stable(v, n)) k--; if(!k){ cout << "TAK" << endl; copy(v.begin(), v.end(), ostream_iterator<long>(cout, " ")); return 0; } } while(next_permutation(v.begin(),v.end())); cout << "NIE"; return 0; } bool is_stable(vector<long> v, long n){ vector<long> rev(n); reverse_copy(begin(v), end(v), begin(rev)); return getInvCount(v,n) == getInvCount(rev,n); } long getSum(long BITree[], long index){ long sum = 0; while (index > 0) { sum += BITree[index]; index -= index & (-index); } return sum; } void updateBIT(long BITree[], long n, long index, long val){ while (index <= n) { BITree[index] += val; index += index & (-index); } } long getInvCount(vector<long> v, long maxElement){ long invcount = 0; vector<long> BIT(maxElement+1, 0); for (long i=v.size()-1; i>=0; i--) { invcount += getSum(BIT.data(), v[i]-1); // accumulate(BIT.begin(), vector.end(), 0); updateBIT(BIT.data(), maxElement, v[i], 1); } return invcount; } |