QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#234943#2789. SortingCamillus20 4ms4352kbC++202.9kb2023-11-02 04:48:152023-11-02 04:48:16

Judging History

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

  • [2023-11-02 04:48:16]
  • 评测
  • 测评结果:20
  • 用时:4ms
  • 内存:4352kb
  • [2023-11-02 04:48:15]
  • 提交

answer

#include "bits/stdc++.h"
#include "sorting.h"
using namespace std;

mt19937 rnd(228);

int findSwapPairs(int N, int _S[], int M, int X[], int Y[], int P[], int Q[]) {
	vector<int> S(_S, _S + N);
	vector<int> D(N);

	bool is_sorted = true;
	for (int i = 0; i < N; i++) {
		D[S[i]] = i;
		if (i != S[i]) {
			is_sorted = false;
		}
	}

	if (is_sorted) {
		return 0;
	}

	auto do_swap = [&](int u, int v) {
		swap(S[u], S[v]);
		swap(D[S[u]], D[S[v]]);
	};


	do_swap(X[0], Y[0]);

	vector<set<int>> cycles;
	vector<int> num(N, -1);

	map<pair<int, int>, int> saved;

	for (int i = 0; i < N; i++) {
		if (num[i] != -1) {
			continue;
		}

		cycles.emplace_back();

		int u = i;
		while (num[u] == -1) {
			num[u] = cycles.size() - 1;
			cycles.back().insert(u);
			u = S[u];
		}
	}
	
	for (int i = 1; i < M; i++) {
		if (cycles.size() == N) {
			P[i - 1] = 0;
			Q[i - 1] = 0;
			return i;
		}

		if (cycles.size() == N - 1) {
			for (int j = 0; j < N; j++) {
				if (j != S[j]) {
					P[i - 1] = j;
					Q[i - 1] = S[j];
					break;
				}
			}
			return i;
		}

		int u = X[i];
		int v = Y[i];

		if (num[u] == num[v]) {
			int x = -1;
			for (int y = 0; y < N; y++) {
				if (y != S[y] && S[y] != u && S[y] != v) {
					x = y;
				}
			}

			if (x == -1) {
				P[i - 1] = 0;
				Q[i - 1] = 0;
				P[i] = 0;
				Q[i] = 0;
				
				return i + 1;
			} else {
				P[i - 1] = x;
				Q[i - 1] = S[x];

				cycles.emplace_back();
				cycles.back().insert(S[x]);
				cycles[num[S[x]]].erase(S[x]);
				num[S[x]] = cycles.size() - 1;

				do_swap(P[i - 1], Q[i - 1]);
			}

			if (u != v) {
				if (rnd() % 2 == 0) {
					swap(u, v);
				}

				do_swap(u, v);
				cycles.emplace_back();
				
				while (num[u] == num[v]) {
					cycles.back().insert(u);
					cycles[num[v]].erase(u);
					num[u] = cycles.size() - 1;
				}
			}
		} else {
			if (u != D[u]) {
				P[i - 1] = u;
				Q[i - 1] = D[u];

				cycles.emplace_back();
				cycles.back().insert(D[u]);
				cycles[num[D[u]]].erase(D[u]);
				num[D[u]] = cycles.size() - 1;

				do_swap(P[i - 1], Q[i - 1]);
			} else if (v != D[v]) {
				P[i - 1] = v;
				Q[i - 1] = D[v];

				cycles.emplace_back();
				cycles.back().insert(D[v]);
				cycles[num[D[v]]].erase(D[v]);
				num[D[v]] = cycles.size() - 1;

				do_swap(P[i - 1], Q[i - 1]);
			} else {
				P[i - 1] = 0;
				Q[i - 1] = 0;

				int x = -1;
				for (int y = 0; y < N; y++) {
					if (y != S[y] && S[y] != u && S[y] != v) {
						x = y;
					}
				}

				if (x == -1) {
					P[i - 1] = X[i];
					Q[i - 1] = Q[i];
					return i + 1;
				} else {
					P[i - 1] = x;
					Q[i - 1] = S[x];

					cycles.emplace_back();
					cycles.back().insert(S[x]);
					cycles[num[S[x]]].erase(S[x]);
					num[S[x]] = cycles.size() - 1;

					do_swap(P[i - 1], Q[i - 1]);
				}
			}
		}
	}
	return M;
}

详细

Subtask #1:

score: 8
Accepted

Test #1:

score: 8
Accepted
time: 0ms
memory: 3768kb

input:

1
0
1
0 0

output:

0

result:

ok correct

Test #2:

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

input:

2
0 1
4
0 0
0 0
0 0
0 0

output:

0

result:

ok correct

Test #3:

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

input:

2
1 0
4
0 0
0 0
0 0
0 0

output:

1
0 1

result:

ok correct

Test #4:

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

input:

3
1 0 2
9
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0

output:

1
0 1

result:

ok correct

Test #5:

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

input:

4
3 2 0 1
16
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0

output:

3
3 1
3 2
0 3

result:

ok correct

Test #6:

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

input:

5
1 4 2 3 0
25
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0

output:

2
1 4
0 1

result:

ok correct

Test #7:

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

input:

5
4 2 1 0 3
25
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0

output:

3
4 3
2 1
0 4

result:

ok correct

Subtask #2:

score: 12
Accepted

Test #8:

score: 12
Accepted
time: 0ms
memory: 4060kb

input:

1
0
30
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0

output:

0

result:

ok correct

Test #9:

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

input:

2
0 1
60
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0

output:

0

result:

ok correct

Test #10:

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

input:

98
82 70 31 12 27 51 84 90 66 8 49 52 74 91 46 7 96 67 63 85 34 50 87 83 58 78 26 39 77 48 2 55 94 25 61 56 53 13 32 86 72 20 37 73 9 93 65 28 18 11 71 59 88 35 76 40 24 36 33 3 17 29 38 5 21 15 79 30 62 92 45 80 64 95 43 75 97 23 16 57 22 60 41 69 0 42 14 10 47 68 19 4 1 6 44 81 54 89
2940
0 0
0 0
...

output:

93
97 89
97 68
97 62
97 38
97 32
97 94
97 44
97 9
97 8
97 66
97 79
97 57
97 36
97 53
97 35
97 56
97 24
97 58
97 33
97 25
97 78
97 16
97 96
97 54
97 76
95 81
95 60
95 17
95 67
95 30
95 2
95 31
95 55
95 40
95 72
95 64
95 21
95 50
95 71
95 80
95 22
95 87
95 10
95 49
95 11
95 52
95 88
95 47
95 28
95 77
...

result:

ok correct

Test #11:

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

input:

99
8 14 3 0 98 74 18 10 50 93 49 23 80 2 54 79 90 31 66 38 81 36 42 83 39 7 9 71 59 62 67 4 68 5 70 72 34 95 25 27 45 61 97 48 29 15 46 12 87 22 26 63 77 84 11 85 30 24 40 78 41 1 96 69 94 19 6 65 33 13 52 75 88 82 86 20 92 43 89 47 28 73 35 32 53 60 21 16 37 58 44 76 57 56 17 55 91 51 64
2970
0 0
0...

output:

92
98 64
98 94
98 17
98 31
98 4
97 51
97 63
97 69
97 13
97 2
97 3
96 91
96 76
96 92
96 57
96 24
96 39
96 27
96 71
96 75
96 20
96 81
96 73
96 82
96 35
96 72
96 88
96 37
96 95
96 55
96 85
96 60
96 41
96 61
96 1
96 14
96 54
96 11
96 23
96 83
96 32
96 68
96 33
96 5
96 74
96 86
96 21
96 36
96 34
96 70
96...

result:

ok correct

Test #12:

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

input:

96
12 91 75 59 44 30 52 63 41 13 93 79 82 60 85 78 50 53 57 8 80 67 34 24 87 17 47 33 83 21 81 40 89 49 32 70 26 9 43 7 11 37 94 36 23 28 90 42 35 25 62 56 20 84 55 45 46 22 77 51 0 69 1 31 54 2 3 58 88 76 5 6 65 92 39 66 15 95 10 27 14 86 72 4 68 18 38 19 64 73 71 74 48 29 61 16
2880
0 0
0 0
0 0
0 ...

output:

95
95 16
95 50
95 62
95 1
95 91
95 74
95 39
95 7
95 63
95 31
95 40
95 11
95 79
95 27
95 33
95 49
95 25
95 17
95 53
95 84
95 68
95 88
95 64
95 54
95 55
95 45
95 28
95 83
95 4
95 44
95 23
95 24
95 87
95 19
95 8
95 41
95 37
95 9
95 13
95 60
94 61
94 69
94 76
94 15
94 78
94 10
94 93
94 29
94 21
94 67
94...

result:

ok correct

Subtask #3:

score: 0
Wrong Answer

Test #13:

score: 16
Accepted
time: 0ms
memory: 3804kb

input:

2
0 1
60
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1

output:

0

result:

ok correct

Test #14:

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

input:

5
0 4 1 3 2
150
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
...

output:

2
4 2
1 4

result:

ok correct

Test #15:

score: -16
Wrong Answer
time: 0ms
memory: 3804kb

input:

96
7 15 12 95 11 50 20 42 81 29 58 80 56 71 63 66 44 6 64 39 2 22 73 1 24 27 69 28 45 25 60 61 5 94 14 65 9 86 68 32 79 37 3 57 34 35 10 88 76 78 21 93 19 53 84 52 4 33 38 55 62 67 77 41 31 48 91 49 51 43 90 8 87 54 16 17 70 46 85 0 75 92 74 47 36 89 30 13 59 26 40 18 82 83 72 23
2880
0 1
0 1
0 1
0 ...

output:

91
0 79
1 23
95 23
95 7
95 42
95 3
94 72
94 87
94 13
94 71
94 8
94 81
94 92
94 82
94 74
94 16
94 44
94 34
94 14
94 63
94 41
94 37
94 86
94 30
94 60
94 62
94 77
94 46
94 10
94 58
94 38
94 68
94 51
94 93
94 83
94 47
94 88
94 59
94 55
94 52
94 19
94 39
94 32
94 5
94 50
94 21
94 22
94 73
94 54
94 84
94 ...

result:

wrong answer the final array is not sorted

Subtask #4:

score: 0
Skipped

Dependency #2:

100%
Accepted

Dependency #3:

0%

Subtask #5:

score: 0
Wrong Answer

Test #28:

score: 0
Wrong Answer
time: 4ms
memory: 4352kb

input:

1800
530 1775 466 953 230 1179 944 752 990 1316 275 1029 158 152 1673 1706 172 115 599 1661 131 699 1112 454 551 1059 291 495 28 67 773 480 839 462 1210 757 879 285 439 3 1429 820 26 1023 942 199 356 625 1705 1421 144 1529 716 7 1485 1027 1599 696 517 1353 456 1389 273 1363 1414 1177 718 41 777 1621...

output:

1732
1799 689
1799 860
235 304
1799 33
1279 1115
1799 462
562 1563
1799 647
1799 770
1799 1014
1799 22
1799 1112
1799 1758
1799 23
1799 454
1799 798
731 1242
214 102
1600 556
862 1126
909 1571
768 517
351 1579
1799 1559
1799 1544
1728 1425
1799 1796
1692 1241
1799 1492
915 879
1799 165
1513 495
964 ...

result:

wrong answer the final array is not sorted

Subtask #6:

score: 0
Skipped

Dependency #5:

0%