#include<bits/stdc++.h> // #include <algorithm> using namespace std; int arr[250001]; // 2D array memo for stopping solving same problem // again //download from http://www.geeksforgeeks.org/number-of-permutation-with-k-inversions/ int memo[1000][1000]; // method recursively calculates permutation with // K inversion int numberOfPermWithKInversion(int N, int K) { // base cases if (N == 0) return 0; if (K == 0) return 1; // if already solved then return result directly if (memo[N][K] != 0) return memo[N][K]; // calling recursively all subproblem of // permutation size N - 1 int sum = 0; for (int i = 0; i <= K; i++) { // Call recursively only if total inversion // to be made are less than size if (i <= N - 1) sum += numberOfPermWithKInversion(N-1, K-i); } // store result into memo memo[N][K] = sum; return sum; } //copied from https://ide.geeksforgeeks.org/index.php int getSum(int BITree[], int index) { int sum = 0; while (index > 0) { sum += BITree[index]; index -= index & (-index); } return sum; } void updateBIT(int BITree[], int n, int index, int val) { while (index <= n) { BITree[index] += val; index += index & (-index); } } int getInvCount(int arr[], int n) { int invcount = 0; int maxElement = 0; for (int i=0; i<n; i++) if (maxElement < arr[i]) maxElement = arr[i]; int BIT[maxElement+1]; for (int i=1; i<=maxElement; i++) BIT[i] = 0; for (int i=n-1; i>=0; i--) { invcount += getSum(BIT, arr[i]-1); updateBIT(BIT, maxElement, arr[i], 1); } return invcount; } void print_permutation(int n) { for(int i = 1; i <= n; i++) { cout << arr[i] << " "; } cout << endl; } // Driver program int main() { int n,k; unsigned long long e; cin >> n >> k; int queue = 0; if(n == 1 || n == 2 || n == 3 || k == 0) { cout << "NIE" << endl; return 0; } long long checker = n*(n-1)/2; e = numberOfPermWithKInversion(n, checker/2); if(checker%2 != 0 || k > e) { cout << "NIE" << endl; } else { for(int i = 1; i <= n; i++) { arr[i] = i; } while ( std::next_permutation(arr+1,arr+n+1) ) { if(getInvCount(arr+1,n) == (checker/2)) { queue++; } if(queue == k) { cout << "TAK" << endl; print_permutation(n); break; } } } 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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | #include<bits/stdc++.h> // #include <algorithm> using namespace std; int arr[250001]; // 2D array memo for stopping solving same problem // again //download from http://www.geeksforgeeks.org/number-of-permutation-with-k-inversions/ int memo[1000][1000]; // method recursively calculates permutation with // K inversion int numberOfPermWithKInversion(int N, int K) { // base cases if (N == 0) return 0; if (K == 0) return 1; // if already solved then return result directly if (memo[N][K] != 0) return memo[N][K]; // calling recursively all subproblem of // permutation size N - 1 int sum = 0; for (int i = 0; i <= K; i++) { // Call recursively only if total inversion // to be made are less than size if (i <= N - 1) sum += numberOfPermWithKInversion(N-1, K-i); } // store result into memo memo[N][K] = sum; return sum; } //copied from https://ide.geeksforgeeks.org/index.php int getSum(int BITree[], int index) { int sum = 0; while (index > 0) { sum += BITree[index]; index -= index & (-index); } return sum; } void updateBIT(int BITree[], int n, int index, int val) { while (index <= n) { BITree[index] += val; index += index & (-index); } } int getInvCount(int arr[], int n) { int invcount = 0; int maxElement = 0; for (int i=0; i<n; i++) if (maxElement < arr[i]) maxElement = arr[i]; int BIT[maxElement+1]; for (int i=1; i<=maxElement; i++) BIT[i] = 0; for (int i=n-1; i>=0; i--) { invcount += getSum(BIT, arr[i]-1); updateBIT(BIT, maxElement, arr[i], 1); } return invcount; } void print_permutation(int n) { for(int i = 1; i <= n; i++) { cout << arr[i] << " "; } cout << endl; } // Driver program int main() { int n,k; unsigned long long e; cin >> n >> k; int queue = 0; if(n == 1 || n == 2 || n == 3 || k == 0) { cout << "NIE" << endl; return 0; } long long checker = n*(n-1)/2; e = numberOfPermWithKInversion(n, checker/2); if(checker%2 != 0 || k > e) { cout << "NIE" << endl; } else { for(int i = 1; i <= n; i++) { arr[i] = i; } while ( std::next_permutation(arr+1,arr+n+1) ) { if(getInvCount(arr+1,n) == (checker/2)) { queue++; } if(queue == k) { cout << "TAK" << endl; print_permutation(n); break; } } } return 0; } |