QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#236522#2789. SortingCamillus36 19ms4396kbC++202.9kb2023-11-04 01:09:422023-11-04 01:09:43

Judging History

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

  • [2023-11-04 01:09:43]
  • 评测
  • 测评结果:36
  • 用时:19ms
  • 内存:4396kb
  • [2023-11-04 01:09:42]
  • 提交

answer

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

mt19937 rnd(228);

bool is_sorted(const vector<int> &s) {
	int n = (int)s.size();
	for (int i = 0; i + 1 < n; i++) {
		if (s[i + 1] < s[i]) {
			return false;
		}
	}
	return true;
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
	int n = N;

	vector<int> p(S, S + N);
	vector<int> d(S, S + N);

	if (is_sorted(p)) {
		return 0;
	}

	swap(p[X[0]], p[Y[0]]);

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

	for (int u = 0; u < n; u++) {
		d[p[u]] = u;

		if (num[u] != -1) {
			continue;
		}

		cycles.emplace_back();
		int x = (int)cycles.size() - 1;
		auto &cycle = cycles.back();

		int v = u;
		while (num[v] == -1) {
			num[v] = x;
			cycle.insert(v);
			v = p[v];
		}
	}

	set<int> not_alone;

	for (int u = 0; u < n; u++) {
		if (p[u] != u) {
			not_alone.insert(u);
		}
	}

	auto do_swap = [&](int u, int v) -> void {
		assert(num[u] == num[v]);
		
		not_alone.erase(u);
		not_alone.erase(v);

		if (p[u] == v || rnd() % 2) {
			swap(u, v);
		}

		swap(d[p[u]], d[p[v]]);
		swap(p[u], p[v]);

		cycles.emplace_back();
		int x = (int)cycles.size() - 1;
		auto &old_cycle = cycles[num[u]];
		auto &new_cycle = cycles.back();

		while (num[u] != x) {
			new_cycle.insert(u);
			old_cycle.erase(u);
			num[u] = x;
			u = p[u];
		}

		if (p[u] != u) {
			not_alone.insert(u);
		}

		if (p[v] != v) {
			not_alone.insert(v);
		}
	};

	for (int i = 0; i < M; i++) {
		P[i] = Q[i] = 0;
	}

	for (int i = 1; i < M; i++) {
		if (not_alone.empty()) {
			return i;
		}

		if (not_alone.size() == 2) {
			int u = *not_alone.begin();
			P[i - 1] = u;
			Q[i - 1] = p[u];
			return i;
		}

		if (X[i] == Y[i]) {
			int u = *not_alone.begin();
			P[i - 1] = u;
			Q[i - 1] = p[u];
			do_swap(u, p[u]);
		} else if (num[X[i]] == num[Y[i]]) {
			if (not_alone.size() == 2) {
				return i + 1;
			}

			for (int u : not_alone) {
				if (p[u] != X[i] && p[u] != Y[i]) {
					P[i - 1] = u;
					Q[i - 1] = p[u]; 
					do_swap(u, p[u]);
					break;
				}
			}

			do_swap(X[i], Y[i]);
		} else {
			int x = X[i];
			int y = Y[i];

			if (p[x] == x && p[y] == y) {
				int u = *not_alone.begin();
				P[i - 1] = u;
				Q[i - 1] = p[u];
				do_swap(u, p[u]);
				
				cycles[num[x]].insert(y);
				cycles[num[y]].erase(y);

				num[y] = num[x];

				swap(d[p[x]], d[p[y]]);
				swap(p[x], p[y]);

				not_alone.insert(x);
				not_alone.insert(y);
			} else {
				if (d[y] != y) {
					swap(x, y);
				}
				
				P[i - 1] = x;
				Q[i - 1] = d[x];

				do_swap(x, d[x]);

				cycles[num[x]].erase(x);
				cycles[num[y]].insert(x);

				num[x] = num[y];

				swap(d[p[x]], d[p[y]]);
				swap(p[x], p[y]);

				not_alone.insert(x);
				not_alone.insert(y);
			}
		}
	}
	return M;
}

详细

Subtask #1:

score: 8
Accepted

Test #1:

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

input:

1
0
1
0 0

output:

0

result:

ok correct

Test #2:

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

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

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

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

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
0 3
0 1
0 2

result:

ok correct

Test #6:

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

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
0 1
0 4

result:

ok correct

Test #7:

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

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

result:

ok correct

Subtask #2:

score: 12
Accepted

Test #8:

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

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

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

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
0 82
0 41
0 20
0 34
0 61
0 29
0 48
0 18
0 63
0 5
0 51
0 59
0 3
0 12
0 74
0 43
0 73
0 95
0 81
0 60
0 17
0 67
0 30
0 2
0 31
0 55
0 40
0 72
0 64
0 21
0 50
0 71
0 80
0 22
0 87
0 10
0 49
0 11
0 52
0 88
0 47
0 28
0 77
0 23
0 83
0 69
0 92
0 1
0 70
0 45
0 93
0 6
0 84
4 27
4 39
4 86
4 14
4 46
4 65
4 15
4 ...

result:

ok correct

Test #11:

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

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
0 8
0 50
0 26
0 9
0 93
0 56
0 30
0 67
0 65
0 19
0 38
0 25
0 7
0 10
0 49
0 22
0 42
0 97
0 51
0 63
0 69
0 13
0 2
0 3
1 14
1 54
1 11
1 23
1 83
1 32
1 68
1 33
1 5
1 74
1 86
1 21
1 36
1 34
1 70
1 52
1 77
1 43
1 48
1 87
1 16
1 90
1 44
1 29
1 62
1 96
1 91
1 76
1 92
1 57
1 24
1 39
1 27
1 71
1 75
1 20
1 8...

result:

ok correct

Test #12:

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

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
0 12
0 82
0 72
0 65
0 2
0 75
0 66
0 3
0 59
0 51
0 56
0 46
0 90
0 71
0 6
0 52
0 20
0 80
0 14
0 85
0 18
0 57
0 22
0 34
0 32
0 89
0 73
0 92
0 48
0 35
0 70
0 5
0 30
0 81
0 86
0 38
0 43
0 36
0 26
0 47
0 42
0 94
0 61
0 69
0 76
0 15
0 78
0 10
0 93
0 29
0 21
0 67
0 58
0 77
0 95
0 16
0 50
0 62
0 1
0 91
0 ...

result:

ok correct

Subtask #3:

score: 16
Accepted

Test #13:

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

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

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
0 4
1 2

result:

ok correct

Test #15:

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

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
1 23
1 15
0 79
2 12
2 56
2 4
2 11
2 80
2 75
2 17
2 6
2 20
3 95
3 23
3 7
3 42
5 50
5 21
5 22
5 73
5 54
5 84
5 36
5 9
5 29
5 25
5 27
5 28
5 45
5 35
5 65
5 48
5 76
5 70
5 90
5 40
5 79
5 66
5 91
5 18
5 64
5 31
5 61
5 67
5 49
5 78
5 85
5 89
5 26
5 69
5 43
5 57
5 33
5 94
5 72
5 87
5 13
5 71
5 8
5 81
5 ...

result:

ok correct

Test #16:

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

input:

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

output:

92
0 2
1 81
1 28
0 42
3 95
3 91
3 20
3 97
3 93
3 26
3 67
3 85
3 37
3 87
3 63
3 74
3 10
3 82
3 80
3 64
3 88
3 52
3 58
3 45
3 5
3 6
3 30
3 36
3 34
3 76
3 75
3 25
3 35
3 47
3 17
3 29
3 44
3 68
3 84
3 21
3 56
3 53
3 24
3 38
3 65
3 77
3 31
3 72
3 94
3 43
3 55
3 48
3 61
4 73
4 70
4 79
4 27
4 15
4 49
4 50
...

result:

ok correct

Test #17:

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

input:

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

output:

97
1 83
2 59
2 88
2 54
2 50
2 42
2 43
2 40
2 92
2 89
2 18
2 47
2 57
2 34
2 15
2 79
2 41
2 84
2 20
2 33
2 24
2 78
2 26
2 13
2 71
2 55
2 31
2 77
2 39
2 51
2 25
2 36
2 28
2 9
2 11
2 60
2 67
2 69
2 86
2 23
2 8
2 48
2 17
2 30
2 74
2 22
2 94
2 97
2 58
2 70
2 76
2 93
2 32
2 80
2 98
2 21
2 16
2 49
2 85
2 29...

result:

ok correct

Test #18:

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

input:

4
0 1 3 2
120
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
0 ...

output:

2
2 3
0 0

result:

ok correct

Subtask #4:

score: 0
Wrong Answer

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Test #19:

score: 18
Accepted
time: 0ms
memory: 3968kb

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 #20:

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

input:

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

output:

0

result:

ok correct

Test #21:

score: -18
Wrong Answer
time: 11ms
memory: 4272kb

input:

481
264 22 307 266 238 227 68 26 13 12 384 162 410 306 374 311 442 38 407 310 280 308 333 146 326 381 367 110 190 33 141 468 153 393 157 415 229 75 276 392 349 348 179 445 317 64 78 20 323 257 292 395 129 259 165 398 151 219 14 116 338 188 243 61 150 84 72 340 155 133 459 464 248 433 124 399 177 62 ...

output:

14430
0 264
0 327
91 347
0 456
144 248
0 185
288 355
345 321
281 413
0 431
393 33
29 328
0 132
350 365
117 96
407 18
82 296
331 312
388 418
410 12
453 151
371 407
232 216
0 375
0 422
232 247
233 159
447 50
105 319
353 298
0 353
0 144
427 33
251 462
130 124
1 22
17 82
293 476
333 293
148 165
299 466
...

result:

wrong answer the final array is not sorted

Subtask #5:

score: 0
Wrong Answer

Test #28:

score: 20
Accepted
time: 5ms
memory: 4396kb

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:

2147
0 530
1387 1600
201 481
0 288
1047 582
0 1704
768 517
1204 1487
0 346
0 1543
0 941
350 1209
1229 1470
870 1134
1210 34
1470 1508
586 695
1554 415
551 24
1302 970
102 215
0 475
436 1391
0 576
0 965
915 879
975 183
598 504
562 1563
0 1091
0 688
513 609
0 492
407 390
0 1480
1310 1157
0 282
1049 10...

result:

ok correct

Test #29:

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

input:

1950
1718 335 1071 714 1080 1828 1472 410 553 1269 297 401 1021 621 1763 528 205 1231 406 5 715 876 1474 1110 653 862 838 291 1092 549 955 1334 1044 300 1070 720 1877 484 1597 1948 1078 28 87 1193 505 1024 1259 63 852 960 633 1573 713 504 15 820 1852 752 1601 1168 471 1903 499 693 1209 1492 56 908 9...

output:

1946
1313 637
0 1718
0 1717
1675 1625
1107 1194
580 1371
0 688
1302 475
0 1910
0 683
0 969
0 625
1793 502
0 692
0 1932
1894 303
0 1788
1239 791
0 1452
0 1531
0 580
0 1787
0 898
0 794
0 1648
0 551
0 884
0 518
0 595
0 1337
0 1890
0 591
0 1801
1352 1158
0 1364
0 198
0 1444
0 1883
0 1942
0 1464
0 265
0 ...

result:

ok correct

Test #30:

score: -20
Wrong Answer
time: 19ms
memory: 4152kb

input:

1853
452 1299 444 1527 510 1801 34 1178 1589 1782 342 1800 735 761 879 999 1122 1772 236 566 1459 1256 1149 1498 1827 1027 715 1377 6 306 1811 950 500 890 196 1825 1568 1157 943 1422 475 1237 1792 1796 1363 1217 889 968 1439 1116 322 795 1088 1358 1833 96 118 543 1337 207 876 1144 805 215 1433 903 1...

output:

5559
1626 106
0 452
0 31
529 637
0 950
725 1641
1743 1824
755 799
1484 642
1638 919
1329 711
205 710
1272 263
1584 119
0 343
786 1711
1477 1039
0 636
1216 647
0 205
0 1417
386 143
1847 627
0 1188
1168 535
1407 1185
746 1246
431 1285
523 543
1614 482
1106 944
0 639
0 461
556 768
0 1383
19 230
0 392
9...

result:

wrong answer the final array is not sorted

Subtask #6:

score: 0
Skipped

Dependency #5:

0%