QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#674697 | #7778. Turning Permutation | Calculatelove# | WA | 0ms | 3956kb | C++14 | 2.2kb | 2024-10-25 17:15:36 | 2024-10-25 17:15:36 |
Judging History
answer
#include <bits/stdc++.h>
typedef long long s64;
const int N = 55;
int n;
s64 k;
bool exist[N];
int ans[N];
int t, val[N];
s64 f[N][N][2];
s64 pre[N], suf[N];
bool solve(int now) {
if (now == n + 1) return 1;
for (int num = 1; num <= n; num ++) {
if (exist[num]) continue;
if (num == 1 || num == n || (!exist[num - 1] && !exist[num + 1]) || (exist[num - 1] && exist[num + 1])) {
exist[num] = 1, ans[now] = num;
t = 0;
for (int i = 1; i <= n; i ++)
if (!exist[i]) val[++ t] = i;
s64 cnt = 0;
if (t == 0) {
return solve(now + 1);
} else if (t == 1) {
cnt = 1;
} else {
int st = 2;
if (val[1] == 1 && val[2] == 2)
st = 3, f[2][1][1] = 1, f[2][2][0] = 1;
else
f[1][1][0] = 1;
for (int i = st; i <= t; i ++) {
int v = val[i];
for (int j = 1; j <= i; j ++) f[i][j][0] = f[i][j][1] = 0;
pre[0] = 0;
for (int j = 1; j <= i - 1; j ++) pre[j] = pre[j - 1] + f[i - 1][j][1];
suf[i] = 0;
for (int j = i - 1; j >= 1; j --) suf[j] = suf[j + 1] + f[i - 1][j][0];
if (exist[v - 1]) {
for (int j = 1; j <= i; j ++)
f[i][j][0] = pre[i - 1] + suf[1];
} else {
for (int j = 1; j <= i; j ++)
f[i][j][0] = pre[j - 1], f[i][j][1] = suf[j];
}
}
for (int j = 1; j <= t; j ++) cnt += f[t][j][0] + f[t][j][1];
}
if (k > cnt)
k -= cnt, exist[num] = 0;
else
return solve(now + 1);
}
}
return 0;
}
int main() {
scanf("%d%lld", &n, &k);
if (!solve(1)) {
puts("-1");
} else {
for (int i = 1; i <= n; i ++)
printf("%d ", ans[i]);
puts("");
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3796kb
input:
3 2
output:
2 1 3
result:
ok 3 number(s): "2 1 3"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3800kb
input:
3 5
output:
-1
result:
ok 1 number(s): "-1"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3888kb
input:
4 6
output:
3 1 2 4
result:
ok 4 number(s): "3 1 2 4"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3688kb
input:
4 11
output:
-1
result:
ok 1 number(s): "-1"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3868kb
input:
3 1
output:
1 3 2
result:
ok 3 number(s): "1 3 2"
Test #6:
score: 0
Accepted
time: 0ms
memory: 3824kb
input:
3 10
output:
-1
result:
ok 1 number(s): "-1"
Test #7:
score: 0
Accepted
time: 0ms
memory: 3744kb
input:
3 52
output:
-1
result:
ok 1 number(s): "-1"
Test #8:
score: 0
Accepted
time: 0ms
memory: 3820kb
input:
3 756
output:
-1
result:
ok 1 number(s): "-1"
Test #9:
score: 0
Accepted
time: 0ms
memory: 3608kb
input:
3 7721
output:
-1
result:
ok 1 number(s): "-1"
Test #10:
score: 0
Accepted
time: 0ms
memory: 3956kb
input:
5 1
output:
1 3 2 5 4
result:
ok 5 number(s): "1 3 2 5 4"
Test #11:
score: 0
Accepted
time: 0ms
memory: 3764kb
input:
5 8
output:
2 4 1 3 5
result:
ok 5 number(s): "2 4 1 3 5"
Test #12:
score: 0
Accepted
time: 0ms
memory: 3696kb
input:
5 85
output:
-1
result:
ok 1 number(s): "-1"
Test #13:
score: 0
Accepted
time: 0ms
memory: 3772kb
input:
5 846
output:
-1
result:
ok 1 number(s): "-1"
Test #14:
score: 0
Accepted
time: 0ms
memory: 3808kb
input:
5 6957
output:
-1
result:
ok 1 number(s): "-1"
Test #15:
score: 0
Accepted
time: 0ms
memory: 3880kb
input:
8 1
output:
1 3 2 5 4 7 6 8
result:
ok 8 numbers
Test #16:
score: 0
Accepted
time: 0ms
memory: 3888kb
input:
8 7
output:
1 3 2 5 7 8 4 6
result:
ok 8 numbers
Test #17:
score: -100
Wrong Answer
time: 0ms
memory: 3624kb
input:
8 71
output:
-1
result:
wrong answer 1st numbers differ - expected: '1', found: '-1'