QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#162693#5199. Amazing Trickkaruna#RE 2ms3680kbC++171.7kb2023-09-03 15:51:472023-09-03 15:51:48

Judging History

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

  • [2023-09-03 15:51:48]
  • 评测
  • 测评结果:RE
  • 用时:2ms
  • 内存:3680kb
  • [2023-09-03 15:51:47]
  • 提交

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 if (cyc[0] == 3) {
				p[0] = 1;
				p[1] = 2;
				p[2] = 0;
			}
			else {
				p[0] = a[0];
				p[1] = a[1];
				p[2] = a[2];
			}
		}
		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++) {
			assert(i != p[i]);
			assert(i != q[i]);
		}
		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: 1ms
memory: 3644kb

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: 2ms
memory: 3616kb

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: 0ms
memory: 3588kb

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

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
Dangerous Syscalls

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:


result: