QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#162677#5199. Amazing Trickkaruna#WA 1ms3592kbC++171.6kb2023-09-03 15:46:372023-09-03 15:46:38

Judging History

This is the latest submission verdict.

  • [2023-09-03 15:46:38]
  • Judged
  • Verdict: WA
  • Time: 1ms
  • Memory: 3592kb
  • [2023-09-03 15:46:37]
  • Submitted

answer

#include <bits/stdc++.h>
#define ff first
#define ss second
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int main() {
	cin.tie(0); ios_base::sync_with_stdio(0);
	int T; cin >> T;
	while (T--) {
		int n; cin >> n;
		vector<int> a(n);
		for (int i = 0; i < n; i++) {
			cin >> a[i];
			--a[i];
		}
		int cyc[3] = {};
		vector<bool> vis(n);
		for (int i = 0; i < n; i++) {
			if (vis[i]) continue;
			int cnt = 0;
			for (int v = i; !vis[v]; v = a[v]) {
				vis[v] = true;
				++cnt;
			}
			cyc[min(2, cnt - 1)]++;
		}
		vector<int> p(n), q(n);
		if (n == 1) {
			cout << "Impossible\n";
			continue;
		}
		else if (n == 2) {
			if (a[0] == 1 && a[1] == 0) {
				cout << "Impossible\n";
				continue;
			}
			else {
				p[0] = 1;
				p[1] = 0;
				q[0] = 1;
				q[1] = 0;
			}
		}
		else if (n == 3) {
			if (cyc[0] == 1 && cyc[1] == 1) {
				cout << "Impossible\n";
				continue;
			}
			else {
				p[0] = 1;
				p[1] = 2;
				p[2] = 0;
				q[0] = 2;
				q[1] = 0;
				q[2] = 1;
			}
		}
		else {
			vector<int> w;
			fill(vis.begin(), vis.end(), false);
			for (int i = 0; i < n; i++) {
				if (vis[i]) continue;
				for (int v = i; !vis[v]; v = a[v]) {
					vis[v] = true;
					w.push_back(v);
				}
			}
			for (int i = 0; i < n; i++) {
				p[w[i]] = w[(i + n - 2) % n];
			}
			for (int i = 0; i < n; i++) {
				q[a[p[i]]] = i;
			}
		}
		cout << "Possible\n";
		for (int i = 0; i < n; i++) cout << p[i] + 1 << ' '; cout << '\n';
		for (int i = 0; i < n; i++) cout << q[i] + 1 << ' '; cout << '\n';
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 1ms
memory: 3580kb

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

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: -100
Wrong Answer
time: 0ms
memory: 3580kb

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
2 3 1 
3 1 2 
Possible
2 3 1 
3 1 2 
Possible
2 3 1 
3 1 2 
Possible
2 3 1 
3 1 2 
Impossible
Possible
2 3 1 
3 1 2 
Impossible
Possible
2 3 1 
3 1 2 
Impossible
Possible
2 3 1 
3 1 2 
Impossible
Possible
2 3 1 
3 1 2 
Possible
2 3 1 
3 1 2 
Impossible
Impossible

result:

wrong answer Incorrect permuations p & q: a[p[q[1]]] = 3 (test case 2)