QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#109421#5199. Amazing Trickgigabuffoon#WA 3ms3724kbC++202.0kb2023-05-28 23:08:272023-05-28 23:08:29

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-28 23:08:29]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:3724kb
  • [2023-05-28 23:08:27]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define all(a) begin(a), end(a)
#define sz(a) int(size(a))
#define rep(i,a,b) for(int i=a;i<(b);++i)
using vi = vector<int>;

const int MAXB = 7;
vector<vi> dranges[MAXB+1];

void gen(vi &a, vi &used, int idx, int n) {
    if(idx == n) {
        dranges[n].push_back(a);
        return;
    }
    for(int i = 0; i < n; i++) if(i != idx and !used[i]) {
        used[i] = true;
        a[idx] = i;
        gen(a, used, idx+1, n);
        a[idx] = -1;
        used[i] = false;
    }
}

void precomp() {
    srand(time(0));
    for(int n = 1; n <= MAXB; n++) {
        vi a(n, -1), used(n);
        gen(a, used, 0, n);
    }
}

void solve(int tc) {
    int N; cin >> N;
    vi A(N), pos(N);
    for(int i = 0; i < N; i++)
        cin >> A[i], --A[i], pos[A[i]] = i;

    vi finP, pPos(N), Q(N);

    if(N <= MAXB) {
        for(vi &P : dranges[N]) {
            rep(i,0,N) pPos[P[i]] = i;
            bool bad = false;
            rep(i,0,N) if((Q[i] = pPos[pos[i]]) == i) { bad = true; break; }
            if(bad) continue;
            finP = P;
            break;
        }
    } else {
        vi P(N); iota(all(P), 0);
        while(true) {
            random_shuffle(all(P));
            rep(i,0,N) pPos[P[i]] = i;
            bool bad = false;
            rep(i,0,N) if((Q[i] = pPos[pos[i]]) == i) { bad = true; break; }
            if(bad) continue;
            break;
        }
        finP = P;
    }

    if(sz(finP)) {
        // we got 'em
        cout << "Possible" << '\n';
        vi P = finP;
        for(int x : P) cout << ++x << ' ';
        cout << '\n';
        for(int x : Q) cout << ++x << ' ';
        cout << '\n';
        return;
    } else
        cout << "Impossible" << '\n';
}

int main() {
    cin.tie(0)->sync_with_stdio(false);
    cin.exceptions(cin.failbit);
    precomp();
    int t = 1;
    cin >> t;
    for(int i = 1; i <= t; i++) solve(i);
    cout.flush(); return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3680kb

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
3 1 2 5 4 
3 5 4 1 2 

result:

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

Test #2:

score: 0
Accepted
time: 2ms
memory: 3708kb

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: 3ms
memory: 3680kb

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: 3660kb

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: 0
Accepted
time: 2ms
memory: 3628kb

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
2 4 1 3 
4 3 2 1 
Possible
2 4 1 3 
4 3 2 1 
Possible
2 1 4 3 
2 4 1 3 
Possible
2 4 1 3 
4 3 2 1 
Possible
3 4 1 2 
4 1 2 3 
Possible
2 3 4 1 
3 4 2 1 
Possible
2 3 4 1 
4 3 1 2 
Possible
2 1 4 3 
3 4 2 1 
Possible
2 3 4 1 
3 4 2 1 
Possible
2 1 4 3 
2 4 1 3 
Possible
2 1 4 3 
4 3 2 1 
Pos...

result:

ok 12/12 are 'Possible' (12 test cases)

Test #6:

score: 0
Accepted
time: 2ms
memory: 3616kb

input:

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

output:

Possible
2 1 4 5 3 
4 3 5 1 2 
Possible
2 1 5 3 4 
2 5 4 3 1 
Possible
3 1 5 2 4 
4 5 2 3 1 
Possible
2 3 1 5 4 
3 4 5 2 1 
Possible
2 4 5 1 3 
4 5 1 3 2 
Possible
2 4 5 1 3 
4 1 5 3 2 
Possible
3 1 2 5 4 
3 5 2 1 4 
Possible
2 1 4 5 3 
5 3 2 1 4 
Possible
3 1 2 5 4 
3 4 1 5 2 
Possible
2 1 5 3 4 
5...

result:

ok 10/10 are 'Possible' (10 test cases)

Test #7:

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

input:

8
6
5 4 3 6 2 1
6
2 1 3 4 5 6
6
1 6 4 5 2 3
6
1 2 3 4 5 6
6
3 6 1 2 5 4
6
2 6 3 5 4 1
6
1 2 4 5 3 6
6
5 3 4 2 6 1

output:

Possible
2 1 4 3 6 5 
5 6 4 1 2 3 
Possible
3 4 1 2 6 5 
4 3 1 2 6 5 
Possible
2 1 4 5 6 3 
2 4 5 6 3 1 
Possible
2 1 4 3 6 5 
2 1 4 3 6 5 
Possible
2 1 4 3 6 5 
4 3 2 5 6 1 
Possible
2 3 4 1 6 5 
5 4 2 6 3 1 
Possible
2 1 4 5 6 3 
2 1 4 6 3 5 
Possible
2 1 4 5 6 3 
5 3 1 6 2 4 

result:

ok 8/8 are 'Possible' (8 test cases)

Test #8:

score: 0
Accepted
time: 2ms
memory: 3720kb

input:

7
7
3 7 6 5 2 1 4
7
6 1 3 2 7 4 5
7
2 4 1 6 7 5 3
7
4 3 5 1 2 6 7
7
3 1 5 7 2 4 6
7
2 3 4 6 7 5 1
7
7 4 2 6 3 1 5

output:

Possible
2 1 4 3 6 7 5 
5 7 2 6 3 4 1 
Possible
3 1 2 5 4 7 6 
3 5 1 7 6 2 4 
Possible
2 3 1 5 4 7 6 
2 3 6 1 7 5 4 
Possible
2 1 4 3 6 7 5 
3 7 1 2 4 5 6 
Possible
3 1 2 5 7 4 6 
3 4 2 7 1 5 6 
Possible
2 3 1 5 4 7 6 
6 3 1 2 7 5 4 
Possible
2 1 4 3 6 7 5 
5 4 7 1 6 3 2 

result:

ok 7/7 are 'Possible' (7 test cases)

Test #9:

score: -100
Wrong Answer
time: 2ms
memory: 3680kb

input:

6
8
6 1 5 3 7 8 4 2
8
4 5 8 1 3 6 2 7
8
6 5 8 2 4 1 3 7
8
4 2 5 3 7 1 8 6
8
5 7 2 6 3 8 4 1
8
4 1 5 3 7 2 8 6

output:

Possible
7 2 3 4 5 6 8 1 
2 7 4 1 3 8 5 6 
Possible
5 2 6 8 7 3 1 4 
8 5 1 7 2 3 4 6 
Possible
5 1 8 7 6 3 4 2 
5 7 4 1 8 2 3 6 
Possible
8 7 3 5 6 4 2 1 
5 7 6 8 3 1 4 2 
Possible
5 2 1 3 8 6 4 7 
5 4 1 8 3 7 2 6 
Possible
3 5 2 8 4 7 6 1 
3 7 5 8 1 4 2 6 

result:

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