#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAX_N = 250000; //1000;
int t[MAX_N];
int n;
long long k;
bool stabilnaPermutacja(int a[], int n) {
long long inw = 0, inw_r=0;
for (int i=0;i<n-1;i++){
for (int j=i+1;j<n;j++){
if (a[i]>a[j])
inw++;
if (a[n-i-1]>a[n-j-1])
inw_r++;
}
}
if (inw == inw_r) {
return true;
} else {
return false;
}
}
int main()
{
cin >> n >> k;
for (int i = 0; i < n; i++)
t[i]=i+1;
long long stabilna_permutacja = 0;
while (true)
{
if (stabilnaPermutacja(t,n)) {
stabilna_permutacja++;
if (stabilna_permutacja == k) {
cout << "TAK\n";
for (int i = 0; i < n; i++)
printf("%d " , t[i]);
return 0;
}
}
int i = n - 1;
while (i > 0 && t[i - 1] >= t[i])
i--;
if (i == 0)
break;
int j = i;
while (j < n && t[j] > t[i - 1])
j++;
j--;
swap(t[i - 1], t[j]);
reverse(t + i, t + n);
}
cout << "NIE";
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 | #include <cstdio> #include <iostream> #include <algorithm> using namespace std; const int MAX_N = 250000; //1000; int t[MAX_N]; int n; long long k; bool stabilnaPermutacja(int a[], int n) { long long inw = 0, inw_r=0; for (int i=0;i<n-1;i++){ for (int j=i+1;j<n;j++){ if (a[i]>a[j]) inw++; if (a[n-i-1]>a[n-j-1]) inw_r++; } } if (inw == inw_r) { return true; } else { return false; } } int main() { cin >> n >> k; for (int i = 0; i < n; i++) t[i]=i+1; long long stabilna_permutacja = 0; while (true) { if (stabilnaPermutacja(t,n)) { stabilna_permutacja++; if (stabilna_permutacja == k) { cout << "TAK\n"; for (int i = 0; i < n; i++) printf("%d " , t[i]); return 0; } } int i = n - 1; while (i > 0 && t[i - 1] >= t[i]) i--; if (i == 0) break; int j = i; while (j < n && t[j] > t[i - 1]) j++; j--; swap(t[i - 1], t[j]); reverse(t + i, t + n); } cout << "NIE"; return 0; } |
English