QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#236470#2789. SortingCamillus0 2ms4072kbC++202.2kb2023-11-03 23:52:582023-11-03 23:52:59

Judging History

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

  • [2023-11-03 23:52:59]
  • 评测
  • 测评结果:0
  • 用时:2ms
  • 内存:4072kb
  • [2023-11-03 23:52:58]
  • 提交

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(p[u], p[v]);
		swap(d[p[u]], d[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 (cycles.size() == n) {
			return i;
		}

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

		if (X[i] == Y[i]) {
			int u = *not_alone.begin();
			do_swap(u, p[u]);

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

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

			do_swap(X[i], Y[i]);
		} else {
			P[i - 1] = X[i];
			Q[i - 1] = Y[i]; 
		}
	}
	return M;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

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

input:

1
0
1
0 0

output:

0

result:

ok correct

Test #2:

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

input:

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

output:

0

result:

ok correct

Test #3:

score: -8
Wrong Answer
time: 0ms
memory: 3716kb

input:

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

output:

2
0 0
0 0

result:

wrong answer the final array is not sorted

Subtask #2:

score: 0
Wrong Answer

Test #8:

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

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: -12
Wrong Answer
time: 1ms
memory: 3776kb

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:

94
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
0 0
4 39
4 86
4 14
4 46
4 65
4 15
4 7
4 90...

result:

wrong answer the final array is not sorted

Subtask #3:

score: 0
Runtime Error

Test #13:

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

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: -16
Runtime Error

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:

Unauthorized output

result:


Subtask #4:

score: 0
Skipped

Dependency #2:

0%

Subtask #5:

score: 0
Wrong Answer

Test #28:

score: 0
Wrong Answer
time: 2ms
memory: 4016kb

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:

5400
0 530
237 1387
235 201
0 288
1279 1047
0 1704
562 768
964 1204
0 346
0 1543
0 941
326 350
1700 1229
431 870
130 1210
1362 1470
731 586
214 1554
1600 551
862 1302
909 102
562 768
351 436
0 475
0 576
1728 915
909 975
1692 598
562 768
1728 915
1140 0
1513 513
964 1204
563 407
513 323
647 1310
1279...

result:

wrong answer the final array is not sorted

Subtask #6:

score: 0
Skipped

Dependency #5:

0%