QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#934763#10214. Sort and \&中国入民小学附属信息I队 (Zicheng Wang, Shubang Liu, Minkai Shao)#AC ✓24ms4012kbC++141.1kb2025-03-15 09:04:082025-03-15 09:04:10

Judging History

This is the latest submission verdict.

  • [2025-03-15 09:04:10]
  • Judged
  • Verdict: AC
  • Time: 24ms
  • Memory: 4012kb
  • [2025-03-15 09:04:08]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;
const int maxn = 1e5 + 5;

int a[maxn], rk[maxn];
vector<pii> op;
inline void sw(int x, int y) { op.emplace_back(make_pair(x, y)), swap(rk[a[x]], rk[a[y]]), swap(a[x], a[y]); }

void work()
{
	int n; cin >> n; op.clear();
	for (int i = 1; i <= n; i++) cin >> a[i], rk[a[i]] = i;
	if (((n + 1) & (-n - 1)) == (n + 1) && a[n] != n) cout << "1\n"; else cout << "0\n";
	int k = 1; while ((1 << k) <= n) k++;
	for (int I = n; I; I--)
	{
		if (__builtin_popcount(I) == 1) continue;
		int i = rk[I];
		while (__builtin_popcount(a[i]) != 1 && i != a[i])
		{
			int x = i ^ ((1 << k) - 1), y = a[i] ^ ((1 << k) - 1), tmp = a[i];
			int c = x & -x, d = y & -y;
			if (!c) c = 1; if (!d) d = 1;
			if (c == d) sw(i, c), sw(c, tmp);
			else sw(i, c), sw(c, d), sw(d, tmp);
		}
	}
	for (int i = 0; i < k; i++)
		while (a[1 << i] != (1 << i)) sw((1 << i), a[1 << i]);
	cout << op.size() << "\n";
	for (auto [x, y] : op) cout << x << " " << y << "\n";
}

int main()
{
	ios::sync_with_stdio(false), cin.tie(0);

	int T; cin >> T;
	while (T--) work();
	return 0;
}

这程序好像有点Bug,我给组数据试试?

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3712kb

input:

1
4
3 1 4 2

output:

0
3
1 2
2 4
4 3

result:

ok all sorted (1 test case)

Test #2:

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

input:

247
31
13 2 10 25 1 28 26 22 30 16 19 29 15 14 24 17 27 4 21 18 9 20 23 11 31 8 7 12 6 5 3
339
214 112 269 250 173 209 235 27 176 239 139 60 251 65 253 230 291 135 191 298 249 23 151 11 45 88 302 83 183 297 317 100 325 172 38 307 182 98 12 70 207 91 130 193 137 243 32 160 285 185 64 199 202 190 211 ...

output:

1
65
25 2
2 1
1 31
9 2
2 1
1 30
9 2
2 13
9 2
2 4
4 3
9 2
2 16
16 15
9 2
2 25
9 2
2 17
12 1
1 2
2 29
12 1
1 2
2 5
12 1
1 4
4 27
12 1
1 6
12 1
1 10
12 1
1 28
7 8
8 1
1 26
7 8
8 1
1 22
7 8
8 1
1 12
16 1
1 24
19 4
4 2
2 21
19 4
4 8
8 7
4 1
1 20
4 1
1 4
4 11
4 1
1 4
4 19
1 2
2 1
1 18
2 1
1 2
2 9
1 4
0
87...

result:

ok all sorted (247 test cases)

Test #3:

score: 0
Accepted
time: 23ms
memory: 4012kb

input:

31
2897
687 310 1393 2586 1050 2751 2328 2223 638 2827 2493 1613 1034 1724 1579 2819 1761 2087 1213 2717 1884 2364 2396 2106 1722 8 2491 473 978 1274 403 2325 1711 156 1227 2696 2069 2419 2472 1787 2487 2761 2822 2505 1739 2307 507 2128 1804 1079 199 347 1462 1891 198 583 2201 1399 940 1958 1830 318...

output:

0
7931
539 4
4 2
2 2897
539 4
4 1
1 2586
539 4
4 1
1 310
539 4
4 16
16 687
539 4
4 2
2 2593
539 4
4 2819
539 4
4 1
1 2530
539 4
4 1
1 2184
539 4
4 2
2 1309
539 4
4 2
2 1117
539 4
4 1
1 2108
539 4
4 1
1 2590
539 4
4 2
2 2721
539 4
4 747
539 4
4 2
2 325
539 4
4 1
1 666
539 4
4 1
1 1292
539 4
4 1
1 197...

result:

ok all sorted (31 test cases)

Test #4:

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

input:

12
7
7 5 3 6 1 4 2
5
5 3 4 1 2
10000
1132 8176 3778 365 3479 4419 5652 7687 7968 2419 81 6716 4933 9926 3015 5642 6300 5227 5671 678 5590 5495 3467 8806 6977 3273 8322 285 2531 4663 4111 8107 7725 9352 1799 4423 958 5421 7445 6745 1769 9250 7262 9443 7285 3548 573 9838 4609 8840 7834 9799 7356 5748 ...

output:

1
10
1 2
2 1
1 7
4 1
1 6
2 1
1 2
2 5
1 4
1 2
0
6
1 2
2 5
1 2
2 4
4 3
1 2
0
27202
3073 2
2 1
1 10000
3073 2
2 1
1 8176
3073 2
2 1
1 1132
3073 2
2 7293
3073 2
2 8585
3073 2
2 1
1 5698
3073 2
2 1
1 4554
3073 2
2 1
1 8500
3073 2
2 1
1 3678
3073 2
2 1
1 6606
3073 2
2 1
1 5750
3073 2
2 1
1 9616
3073 2
2 9...

result:

ok all sorted (12 test cases)

Extra Test:

score: 0
Extra Test Passed