#include "bits/stdc++.h"
using namespace std;
#define int long long
#define endl endl
const int mod = 1e9 + 7;
const int maxn = 1e6;
int n, s, arr[maxn];
vector < pair < int, int >> ans;
void s1()
{
for (int i = n - 1; i >= 0; i--)
{
int p = -1;
for (int j = 0; j <= i; j++)
{
if (p == -1 || arr[p] < arr[j]) p = j;
if (j) ans.push_back({j - 1, j});
}
for (int j = p + 1; j <= i; j++) ans.push_back({j, j - 1});
for (int j = p - 2; j >= 0; j--) ans.push_back({j, j + 1});
for (int j = p; j + 1 < n; j++) swap(arr[j], arr[j + 1]);
}
for (int i = n - 2; i >= 0; --i) ans.push_back({i, i + 1});
}
void s2()
{
for(int i = 19; i >= 0; i--)
{
for(int j = 0; j < n - 1; j++)
{
if(arr[j + 1] >= (1 << (i + 1))) break;
if(!(arr[j] & (1 << i))) continue;
if(!(arr[j + 1] & (1 << i))) ans.push_back({j + 1, j}), arr[j + 1] ^= arr[j];
ans.push_back({j, j + 1});
arr[j] ^= arr[j + 1];
}
}
}
void solve()
{
cin >> n >> s;
for(int i = 0; i < n; i++) cin >> arr[i];
int arr2[n];
if(s == 1) s1();
else s2();
cout << ans.size() << endl;
for(auto [i, j] : ans) cout << i + 1 << " " << j + 1 << endl;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int tt = 1;
//cin >> tt;
while(tt--) solve();
}
Compilation message
xorsort.cpp: In function 'void solve()':
xorsort.cpp:53:9: warning: unused variable 'arr2' [-Wunused-variable]
53 | int arr2[n];
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
3 ms |
348 KB |
Output is correct |
4 |
Correct |
29 ms |
988 KB |
Output is correct |
5 |
Correct |
31 ms |
992 KB |
Output is correct |
6 |
Correct |
29 ms |
988 KB |
Output is correct |
7 |
Correct |
24 ms |
988 KB |
Output is correct |
8 |
Correct |
24 ms |
992 KB |
Output is correct |
9 |
Correct |
23 ms |
988 KB |
Output is correct |
10 |
Correct |
24 ms |
992 KB |
Output is correct |
11 |
Correct |
24 ms |
992 KB |
Output is correct |
12 |
Correct |
32 ms |
988 KB |
Output is correct |
13 |
Correct |
24 ms |
1056 KB |
Output is correct |
14 |
Correct |
24 ms |
1056 KB |
Output is correct |
15 |
Correct |
24 ms |
992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
3 ms |
348 KB |
Output is correct |
4 |
Correct |
29 ms |
988 KB |
Output is correct |
5 |
Correct |
31 ms |
992 KB |
Output is correct |
6 |
Correct |
29 ms |
988 KB |
Output is correct |
7 |
Correct |
24 ms |
988 KB |
Output is correct |
8 |
Correct |
24 ms |
992 KB |
Output is correct |
9 |
Correct |
23 ms |
988 KB |
Output is correct |
10 |
Correct |
24 ms |
992 KB |
Output is correct |
11 |
Correct |
24 ms |
992 KB |
Output is correct |
12 |
Correct |
32 ms |
988 KB |
Output is correct |
13 |
Correct |
24 ms |
1056 KB |
Output is correct |
14 |
Correct |
24 ms |
1056 KB |
Output is correct |
15 |
Correct |
24 ms |
992 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
24 ms |
992 KB |
Output is correct |
18 |
Correct |
41 ms |
1496 KB |
Output is correct |
19 |
Correct |
41 ms |
1500 KB |
Output is correct |
20 |
Correct |
41 ms |
1500 KB |
Output is correct |
21 |
Correct |
42 ms |
1496 KB |
Output is correct |
22 |
Correct |
41 ms |
1500 KB |
Output is correct |
23 |
Correct |
41 ms |
1500 KB |
Output is correct |
24 |
Correct |
42 ms |
1500 KB |
Output is correct |
25 |
Correct |
41 ms |
1500 KB |
Output is correct |
26 |
Correct |
58 ms |
1496 KB |
Output is correct |
27 |
Correct |
44 ms |
1500 KB |
Output is correct |
28 |
Correct |
42 ms |
1500 KB |
Output is correct |
29 |
Correct |
42 ms |
1496 KB |
Output is correct |
30 |
Correct |
41 ms |
1496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
6 ms |
708 KB |
Output is correct |
5 |
Correct |
32 ms |
1272 KB |
Output is correct |
6 |
Correct |
44 ms |
1244 KB |
Output is correct |
7 |
Correct |
35 ms |
1244 KB |
Output is correct |
8 |
Correct |
31 ms |
1240 KB |
Output is correct |
9 |
Correct |
31 ms |
1240 KB |
Output is correct |
10 |
Correct |
34 ms |
1248 KB |
Output is correct |
11 |
Correct |
32 ms |
1200 KB |
Output is correct |
12 |
Correct |
33 ms |
1172 KB |
Output is correct |
13 |
Correct |
34 ms |
1240 KB |
Output is correct |
14 |
Correct |
33 ms |
1244 KB |
Output is correct |
15 |
Correct |
64 ms |
1496 KB |
Output is correct |