QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#511498#5199. Amazing TrickRPChe_WA 1ms5904kbC++142.0kb2024-08-09 23:14:402024-08-09 23:14:44

Judging History

你现在查看的是最新测评结果

  • [2024-08-09 23:14:44]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:5904kb
  • [2024-08-09 23:14:40]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 5;
int n, a[N], p[N], q[N], rva[N], rvp[N], vis[N], tot, t[N];
vector<int> P[] = {{0, 2, 3, 1}, {0, 3, 1, 2}};

void dfs(int x) {
    if (vis[x]) return;
    vis[x] = 1;
    t[++tot] = x;
    dfs(a[x]);
}

bool solve() {
    if (n == 1) {
        return 0;
    } else if (n == 2) {
        if (a[1] == 1 && a[2] == 2) {
            p[1] = q[1] = 2, p[2] = q[2] = 1;
            return 1;
        } else {
            return 0;
        }
    } else if (n == 3) {
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                int flag = 1;
                for (int k = 1; k <= 3; k++) {
                    flag &= a[P[i][P[j][k]]] == k;
                }
                if (flag) {
                    p[1] = P[i][1], p[2] = P[i][2], p[3] = P[i][3];
                    q[1] = P[j][1], q[2] = P[j][2], q[3] = P[j][3];
                    return 1;
                }
            }
        }
        return 0;
    }
    for (int i = 1; i <= n; i++) {
        if (!vis[i]) {
            dfs(i);
        }
    }
    for (int i = 1; i <= n; i++) {
        int nex = i + 2;
        if (nex > n) {
            nex -= n;
        }
        rvp[t[i]] = t[nex];
    }
    for (int i = 1; i <= n; i++) {
        q[i] = rvp[rva[i]];
        p[rvp[i]] = i;
    }
    return 1;
}

int main() {
    int T; scanf("%d", &T);
    while (T--) {
        scanf("%d", &n), tot = 0;
        for (int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
            rva[a[i]] = i;
            vis[i] = 0;
        }
        if (solve()) {
            printf("Possible\n");
            for (int i = 1; i <= n; i++) {
                printf("%d ", p[i]);
            }
            printf("\n");
            for (int i = 1; i <= n; i++) {
                printf("%d ", q[i]);
            }
            printf("\n");
        } else {
            printf("Impossible\n");
        }
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 4096kb

input:

4
2
2 1
3
1 2 3
4
2 1 4 3
5
5 1 4 2 3

output:

Impossible
Possible
2 3 1 
3 1 2 
Possible
3 4 1 2 
4 3 2 1 
Possible
4 3 1 5 2 
5 1 4 2 3 

result:

ok 3/4 are 'Possible' (4 test cases)

Test #2:

score: 0
Accepted
time: 0ms
memory: 3780kb

input:

50
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1

output:

Impossible
Impossible
Impossible
Impossible
Impossible
Impossible
Impossible
Impossible
Impossible
Impossible
Impossible
Impossible
Impossible
Impossible
Impossible
Impossible
Impossible
Impossible
Impossible
Impossible
Impossible
Impossible
Impossible
Impossible
Impossible
Impossible
Impossible
Imp...

result:

ok 0/50 are 'Possible' (50 test cases)

Test #3:

score: 0
Accepted
time: 1ms
memory: 5904kb

input:

25
2
2 1
2
2 1
2
2 1
2
1 2
2
2 1
2
1 2
2
1 2
2
1 2
2
1 2
2
1 2
2
2 1
2
2 1
2
1 2
2
1 2
2
2 1
2
2 1
2
2 1
2
1 2
2
1 2
2
1 2
2
2 1
2
2 1
2
2 1
2
2 1
2
2 1

output:

Impossible
Impossible
Impossible
Possible
2 1 
2 1 
Impossible
Possible
2 1 
2 1 
Possible
2 1 
2 1 
Possible
2 1 
2 1 
Possible
2 1 
2 1 
Possible
2 1 
2 1 
Impossible
Impossible
Possible
2 1 
2 1 
Possible
2 1 
2 1 
Impossible
Impossible
Impossible
Possible
2 1 
2 1 
Possible
2 1 
2 1 
Possible
2 ...

result:

ok 11/25 are 'Possible' (25 test cases)

Test #4:

score: 0
Accepted
time: 0ms
memory: 3884kb

input:

16
3
3 2 1
3
3 1 2
3
3 1 2
3
2 3 1
3
3 1 2
3
2 1 3
3
2 3 1
3
3 2 1
3
3 1 2
3
2 1 3
3
1 2 3
3
1 3 2
3
3 1 2
3
3 1 2
3
3 2 1
3
2 1 3

output:

Impossible
Possible
3 1 2 
3 1 2 
Possible
3 1 2 
3 1 2 
Possible
2 3 1 
2 3 1 
Possible
3 1 2 
3 1 2 
Impossible
Possible
2 3 1 
2 3 1 
Impossible
Possible
3 1 2 
3 1 2 
Impossible
Possible
2 3 1 
3 1 2 
Impossible
Possible
3 1 2 
3 1 2 
Possible
3 1 2 
3 1 2 
Impossible
Impossible

result:

ok 9/16 are 'Possible' (16 test cases)

Test #5:

score: -100
Wrong Answer
time: 0ms
memory: 4044kb

input:

12
4
2 4 1 3
4
2 4 1 3
4
1 3 2 4
4
2 4 1 3
4
4 1 2 3
4
2 4 3 1
4
1 3 4 2
4
3 4 2 1
4
2 4 3 1
4
1 3 2 4
4
3 4 1 2
4
1 3 4 2

output:

Possible
4 3 2 1 
2 4 1 3 
Possible
4 3 2 1 
2 4 1 3 
Possible
3 4 1 2 
3 1 4 2 
Possible
4 3 2 1 
2 4 1 3 
Possible
3 4 1 2 
4 1 2 3 
Possible
4 3 2 1 
1 4 2 3 
Possible
3 4 1 2 
3 2 4 1 
Possible
2 1 4 3 
3 4 2 1 
Possible
4 3 2 1 
1 4 2 3 
Possible
3 4 1 2 
3 1 4 2 
Possible
2 1 4 3 
4 3 2 1 
Pos...

result:

wrong answer Permutation has fixed point: 1 (test case 6)